<$BlogRSDUrl$>

Thursday, March 25, 2004

HttpModule to keep post-back scroll position 

Philip Rieck

One of the things I hate about the joy of postbacks is losing my scroll position.  On a page that's a bit longer than the window, if I have an auto-postback combobox (for example), the user selects an item, the page posts back, and poof! they're back at the top of the page.

On a corporate intranet the post back is nice and fast, and many times the user has no clue that it happened....  Except for the fact that the page just "jumped" back to the top.

Because of this I (as many people) write my controls to re-position, or I have page logic to re-position after a postback. There are articles on CodeProject with controls for keeping the scroll position,   one on DotNetJunkies that isn't quite so pre-packaged, but still good, and several tutorials.  The problem then is that the behavior is coded in everywhere, or the control must be included on each page, or the page must be derived from something other than the Page class, or something else..  I wanted a fire and forget solution.

So here's what I created late last night to un-vex myself -- an HTTPModule that adds scroll position retention across postbacks to all pages.  It's implemented as a filter that adds javascript to the pages after they are output.  I've tested it only on IE 6, as that's the browser my client site's intranet uses.  

The nice thing about this (to me) is that three lines in your web.config instantly gives you scroll position retention across postbacks on every page in your application.  No muss, and not a whole lot of fuss.


Comments: Post a Comment

This page is powered by Blogger. Isn't yours?