"None can stop the rising sun, clouds can hide for a while........" -Ravi

Wednesday, September 23, 2009

Maintain Scroll Position in ASP.NET 2.0

This article explains, how to maintain current page scroll position across page postback events.

When working with large data in gridview, Scroll down the page and doing some thing that causes a postback, Then page automatically set back to the top position rather than staying at the current scroll position.

Smartnavigation (Smartnavigation = true) feature was using in .NET 1.X frame work. But It had only worked in IE browser.

It has been resolved in .NET 2.0 by using MaintainScrollPositionOnPostback page directive attribute.
This attribute can add to the pages in three ways.

1) Set Globally ( Set it in Web.Config in root level)

<pages theme="DefaultSkin" maintainScrollPositionOnPostBack="true" >

2) Page Level Scrolling Mainatanance
In ASP.NET Page:

<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" %>

3) Set in Programatically:
Import the Name space System.Web.UI, like in C#:

using System.Web.UI;

Set MaintainScrollPositionOnPostback true in Page load

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
Page.MaintainScrollPositionOnPostBack = true;