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. :(

# Maintain Scroll Position On Page Refresh Using ASP.NET or jQuery 9/14/2009 7:26 AM Web Dev .NET

Maintain Scroll Position On Page Refresh Using ASP.NET or jQuery

# Maintain Scroll Position on Postback in ASP.NET 2.0 &laquo; Emilio Vick Blog :-) 12/2/2009 6:33 AM Pingback/TrackBack

Maintain Scroll Position on Postback in ASP.NET 2.0 &laquo; Emilio Vick Blog :-)

# re: Maintain Scroll Position on Postback in ASP.NET 2.0 12/24/2009 9:27 AM Rob

doesn't work with asynchronous postback

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.
<February 2010>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51617.1%
Wednesday 55318.4%
Thursday 58019.2%
Friday 54718.2%
Saturday 1886.2%
Total 3013100.0%

Hour1Total% of Total
12:00 AM 772.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1806.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1876.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1073.6%
8:00 PM 923.1%
9:00 PM 882.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3013100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.08477
Wednesday 7.47680
Thursday 6.25675
Friday 5.02462
Saturday 4.78177
Total 5.413013

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.02295
10:00 AM 5.63276
11:00 AM 4.20193
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 4.00108
5:00 PM 6.04169
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.413013

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles