Auto Ad Code

Monday, February 21, 2011

Maintain scroll position on postback (ASP.net 1.1, 2.0, 3.5, 4.0)

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.

1 comment:

  1. MaintainScrollPositionOnPostback = "true" does not work when an asp:Button cause the postback on ASP.NET 3.5. How can we maintain the position when a Button does the postback.

    ReplyDelete