Maintain Scroll Position after Postback

When Web pages are posted back to the server, the user is returned to the top of the page. On long Web pages, this means that the user has to scroll the page back to the last position on the page.

In ASP.net, you can maintain the scroll position after postback. If you set Page.MaintainScrollPositionOnPostBack  to true, your last scroll position will not be lost . Try the following code,

<html xmlns=”http://www.w3.org/1999/xhtml“>
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div style=”height:300px;border:1px solid green;”>
    Green
    </div>
    <div style=”height:300px;border:1px solid red;”>
    Red
    </div>
    <div style=”height:300px;border:1px solid blue;”>
    Blue
    </div>
    <div style=”height:300px;border:1px solid yellow;”>
    Yellow
    <asp:Button ID=”btnSubmit” runat=”server” OnClick=”btn_Clcik” Text=”Submit” />
    </div>
    </form>
</body>
</html>

Codebehind

public partial class tv_MaintainScrollPos : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.MaintainScrollPositionOnPostBack = true;
}

protected void btn_Clcik(object sender, EventArgs e)
{

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s