The solution? Place a server control at the bottom of the page with CSS properties that prevent it from physically being viewed, don't affect scroll bars, and yet can still have focus shifted to it.
So begin by placing an ASP.NET button (or any other small server control) at the bottom of your markup, and add some inline styling to be most compact and streamlined if you wish:
Notice above the styling places it off the page, but the button will not create any lengthy scroll bars, etc. Lastly, add the code server side on the 'Finally' of the button click event to shift focus to the bottom of the page:
<!--Button will render, but not actually be visible to the client-->
<asp:Button ID="ui_btnPageBottom" runat="server" Text="Button" style="margin-left:-999px;" />
That's it! I am sure there are at least 20 ways to do this, including a purely client side solution with JavaScript, but this was 2 lines of code to accomplish what I needed.
Try
'Code
Finally
'Regardless of outcome, set focus to the bottom of the page
Page.SetFocus(Me.ui_btnPageBottom.ClientID)
End Try
Thanks a lot :)
ReplyDeleteGreat...
ReplyDeleteYou Solved my promblem
Page.SetFocus(Me.ui_btnPageBottom.ClientID)
ReplyDeleteWhat is this 'Me' stands for?
The 'Me' keyword refers to the specific instance of a class or structure in which the code is currently executing. In my case it is the instance of the page with the Button control, so 'Me' refers to the page's instance.
ReplyDeleteThanks Allen. That's helped me so much. Appreciate it.
ReplyDeleteHi Allen,
ReplyDeleteI am also trying to do the same but when my scenario is after clicking on submit the page should be scrolled to top.
I tried your solution but what happens is it scrolls but then scrolls back to the bottom because of the postback.
I already have Page.MaintainScrollPositionOnPostBack = False in my code.
Any other workarounds?
Thank you
Thanks Allen,
ReplyDeleteYou saved my day.
Code helped a alot
great, solved my problem
ReplyDeleteWith all the other solutions I've tried from many other different posts, this one proved to be the only one that actually worked. Thanks!
ReplyDelete--patrick
It works.
ReplyDeleteThanks a lot
It worked, Thanks a lot
ReplyDeleteYou da man!! Thanks for sharing.
ReplyDeleteThe control you want to scroll to doesn't necessarily have to be hidden as in my case.
Thanks Sir You solved my problem which was Modelpopup Extender was not showing in center after postback due to scroll position after using ur trick it is showing in center of screen :)
ReplyDeleteThanks a lot. It helped me
ReplyDeleteTried many different things, updatepanels, javascript etc. This was the solution that did what I needed which was go to the bottom of the page on particular postback where Finish button was located for page. My appliction is complex with masterpage and launched pages from gridview link and an updatepanel for page as well as update panels in custom controls on page. Javascript was being called but not working. My guess is the update panel was overriding javascript action. Thank-you!
ReplyDeletePerfecto!Thanks
ReplyDeletethanks Sir, perfect
ReplyDelete