Dismissile Dismissile - 2 months ago 37
ASP.NET (C#) Question

ASP.net Postback - Scroll to Specific Position

I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page. When the page refreshes, I would like to set focus and scroll down to this section of the page.

I tried doing

txtField.Focus()


in my code behind and it will set focus and try to scroll there, but then scrolls right back to the top. The focus is still on my text box but the position of the screen is at the very top. The Link is at the top of the screen which is causing the postback. I want to scroll to the very bottom of the screen. It does this briefly and then scrolls right back to the top.

I have tried setting

Page.MaintainScrollPositionOnPostback = false;


but that doesn't seem to help either.

Is there some way I can force it to go to a specific position?
Is it possible to add an anchor tag to the URL when I postback using a button or link button?

Answer

Page.MaintainScrollPositionOnPostBack = true; should take you back to the same position on the screen, but you could use AJAX, or you could use SetFocus() to focus on a specific control after the postback:

http://msdn.microsoft.com/en-us/library/ms178232.aspx