A recent project I’ve been working on is a knowledge base with dynamic data structures. As such, all of the controls placed on the update page are dynamically created at runtime.
There is a drop down list for country and state, with the country list causing a post back to fill the state list. We needed the page to scroll back to the country list after postback to relieve the user of having to scroll down two pages.
It didn’t take much research to find the Page directive MaintainScrollPositionOnPostback=”true”. While this did get the page to scroll back to the drop down list, it also caused the page to scroll back to the submit button when there was a validation error (all done on the server side). Hey, no problem, I’ll just set that directive programatically only when the post back is caused by that drop down list.
I found a nice article by Peter Bromberg on a way to determine the control that causes a post back at http://www.eggheadcafe.com/articles/20050609.asp. I got null exceptions when trying to return the control, so I changed the code slightly to just return a string that describes the type of control.
But there is a scroll function, and by calling scroll(0, [saved y value]) I was able to finally get the page to scroll back only to the drop down list.
The post back control function, in App_Data/Global.asax.cs:
**The C# snippit placed in the Page OnInit function:**
<preclass="code"><spanstyle="color:green;">// hidden field to store scroll location </span><spanstyle="color:#2b91af;">Page</span>.ClientScript.RegisterHiddenField(<spanstyle="color:#a31515;">"__SCROLLLOC"</span>, <spanstyle="color:#a31515;">"0"</span>);