Scott on Writing

Musings on technical writing...

Maintain Scroll Position on Postback in ASP.NET 2.0

When working with a lengthy web page that spans many vertical “pages,” scrolling down and then doing something that causes a postback results with the posted back page sitting back up at the top rather than auto-scrolling down to the location where the postback was triggered. With ASP.NET 1.x, this problem can be easily overcome with a bit of JavaScript and server-side code. See Maintaining Scroll Position on Postback for more information.

ASP.NET 2.0 makes maintaining scroll position on postback much easier, although it's not a well-document or oft-discussed feature. When I show this little tip at User Group talks, to clients, or to my class, I'd say more than 90% of the people didn't know about this, are impressed, and wonder aloud, “Why isn't this common knowledge?”

Anyway, to have a single page remember scroll position on postback, simply set the MaintainScrollPositionOnPostback attribute in the @Page directive to True. That's it! The page will then automagically inject the needed JavaScript and server-side logic to make this feature a reality.

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

Alternatively, you can enable this feature for all pages in the site by setting it in the <pages> element in Web.config.

<pages maintainScrollPositionOnPostBack="true" />

See my article Client-Side Enhancements in ASP.NET 2.0 for more information on this feature, as well as additional client-side features added to ASP.NET 2.0!

posted on Thursday, December 14, 2006 9:33 AM

Feedback

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 12/14/2006 11:08 AM Michael Shorten

Good stuff! I had futzed about with that, but it doesn't work when you are writing a control that you need to remember the scroll position for, say a Tree View or some other composite custom object. I wrote my own custom control that creates a scrollable div that remembers the scroll position - injects its own javascript to manage it.
michael-dot-shorten-at-mb-dash-limited-dot-com

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 12/14/2006 11:15 AM Brian Hampson

Michael, this is exactly the problem we've run into as well.

It would be nice if scrollable controls had a "MaintainScrollPositionOnPostBack" as well

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 12/15/2006 2:27 AM Vikram

Yes the same problem with me. When u are working with usercontrol(Which I am most of the time) the function does not work

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 12/20/2006 2:05 PM Mark

I was looking for code to do this today then found this, thanks!

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 5/13/2007 7:27 PM Luis

Thanks for this, you save my day.

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 6/23/2008 11:54 PM uzwa

is there any work around ?

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 6/22/2009 12:39 PM Doug

I'd say it's fairly well known by now, however it just doesn't work reliably, especially where AJAX is involved. :(

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2009>
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Comment Stats

DayTotal% of Total
Sunday 2046.9%
Monday 42314.3%
Tuesday 50116.9%
Wednesday 54518.4%
Thursday 57219.3%
Friday 53618.1%
Saturday 1856.2%
Total 2966100.0%

Hour1Total% of Total
12:00 AM 752.5%
1:00 AM 802.7%
2:00 AM 672.3%
3:00 AM 812.7%
4:00 AM 642.2%
5:00 AM 1234.1%
6:00 AM 1153.9%
7:00 AM 1755.9%
8:00 AM 1876.3%
9:00 AM 1565.3%
10:00 AM 1866.3%
11:00 AM 1926.5%
12:00 PM 1996.7%
1:00 PM 1846.2%
2:00 PM 1675.6%
3:00 PM 1344.5%
4:00 PM 1153.9%
5:00 PM 1063.6%
6:00 PM 993.3%
7:00 PM 1063.6%
8:00 PM 903.0%
9:00 PM 842.8%
10:00 PM 893.0%
11:00 PM 923.1%
Total 2966100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.91157
Monday 4.92379
Tuesday 4.21471
Wednesday 7.42668
Thursday 6.53666
Friday 5.17450
Saturday 4.73175
Total 5.522966

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 4.0048
8:00 AM 4.29133
9:00 AM 6.04290
10:00 AM 5.83274
11:00 AM 4.36192
12:00 PM 6.44348
1:00 PM 3.14132
2:00 PM 5.04227
3:00 PM 7.97303
4:00 PM 3.8199
5:00 PM 6.00168
6:00 PM 4.56114
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.522966

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles