Retaining Anchor Position on ASP.NET Postback

Posted by William on Apr 8, 2009

I ran into a small problem the other day that although annoying, proved easy enough to fix. The problem arose because I was using JQuery UI tabs to allow multiple pageable gridviews to be managed on the one page. Whenever the gridview was paged a postback was initiated and the gridview paged as expected, however the selected tab state was not maintained.

This is because .NET posts back on the same url, including the querystring…but not associated anchors. This does make sence because the postback url is set when the page is rendered on the server.

The solution was to register a small piece of javascript in my gridview’s pagechanged event handler that sets the browser’s anchor location.

          this.GetType(), "anchor", "location.hash = '#yourtab';", true);