Tuesday, June 9, 2009

Retaining last scroll position after postback

When a request is submitted to the server through any auto-postback enabled control, after the postback, by default user is returned to the top of the page. Last week we have a requirement to scroll down the user automatically to the last position on the page after the postback.

After a bit of GOOGLING, we came to know that we can do this in different levels, like Application level, Page level or even using our code. MaintainScrollPositionOnPostBack page property should be used to acheive this.

1. Application level: This way the functionality will be applicable to all the pages in the application/website. Add the following attribute to the pages node in your respective web.config file:

<pages maintainScrollPositionOnPostBack="true">

2. Page Level: This way the functionality will be applicable to a particular page in the application/website. Open the aspx page and set the property in the page directive as shown below:

<%@ Page MaintainScrollPositionOnPostback="true" ... />

3. Code level: To set this property programmatically(C# code below), you can write the following line of code:

Page.MaintainScrollPositionOnPostBack = true;

Note: We have tested this in IE 6+ and FF 3+. In both the cases this works fine.

0 comments: