Thursday, 15 November 2012

Maintain Scroll Position of the Browser on Postback

Maintain Scroll Position of the Browser on Postback


We often face this issue that our control is placed somewhere down in page and we have reached the control using the scrollbar of browser.
Now there is some postback event associated with that control (that control can be radiobutton, listbox, dropdown list, chekbox, button etc) and when that even is fired the page comes back with its scroll position at top.
Frustrated? Yes obviously its such a situation that now you have to again scroll down to the control.

ASP .Net 2.0 and above

Microsoft (ASP.Net) has provided a very simple and smart solution for this. You just have to set the MaintainScrollPositionOnPostback property is page directive (Top most line of HTML view of aspx page).
Let say your page is default.aspx then following will be the Page directive in order to maintain the scroll position.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Inherits="_default" MaintainScrollPositionOnPostback="true" %>

ASP .Net 1.1

For ASP .Net 1.1 you can use following. i.e. smartnavigation attribute in the page directive.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Inherits="_default" SmartNavigation="True" %>


Now whenever page will be posted back from some control which is somewhere scrolled down then after postback the position of cursor (control) will be maintained. 

If you are getting Errors even after implementing the same.. Refer this thread:

http://forums.asp.net/p/1881999/5298698.aspx/1?p=True&t=634962333386536866

2 comments:

  1. Hello, excuse me i didnt get it till now. Please i need more explanation

    ReplyDelete
  2. Maintaining Scroll Position is different from getting button Code for Navigating to the Top of the Page.

    Bye the way,I have answered your Question over here:
    http://forums.asp.net/post/5239748.aspx

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top