I have a codebehind file, in which it does a:
Response.Redirect(Request.RawUrl);
after i have updated something in the database. (when a ment for the topic is stored)
I wish to make the page autoscroll, to the bottom at the page, when the response is triggered. Can someone please tell me how?
I have a codebehind file, in which it does a:
Response.Redirect(Request.RawUrl);
after i have updated something in the database. (when a ment for the topic is stored)
I wish to make the page autoscroll, to the bottom at the page, when the response is triggered. Can someone please tell me how?
Share Improve this question edited Nov 20, 2012 at 12:06 Satinder singh 10.2k18 gold badges64 silver badges102 bronze badges asked Nov 20, 2012 at 11:46 AndersAnders 1031 gold badge3 silver badges10 bronze badges 2- forums.asp/t/1595228.aspx/1 – Karthik Commented Nov 20, 2012 at 11:49
- Ohh... I thought that i could do a "scrolltosomething" in the same line as the response.redirect one. – Anders Commented Nov 20, 2012 at 11:55
6 Answers
Reset to default 2Scrolling to the bottom of the page can be done by using javascript. Below is the javascript code to do the same. Please place the below code at the end of the page before body tag ending.
<script>
window.scrollTo(0, document.body.clientHeight);
</script>
What about adding an anchor to the new ment.
<a name="NewComment">The ment</a>
Then let your redirect point to that anchor
Response.Redirect(Request.RawUrl + "#NewComment");
That should make your browser scroll to that anchor
Scrolling to the bottom of the page is client side not server side. C# is server side. In order to scroll you will have to add some javascript to the page to do it for you.
Typically, this is implemented as follows, a javascript routine is written that looks at the URL. If there is some specific information in the url (eg &scroll2bottom=true) then the javascript performs that action. This will also let you scroll to a specific page element.
I use this function, maybe it is useful for you. It works with coordinates and delay seconds for scroll. Trying with different coordinates will make the scroll stop where you want.
Declare this function in your js
function WindowsScrollTopAnimado(coordinate,miliseconds) {
$('html, body').animate({scrollTop:coordinate}, miliseconds);
}
Call it from server side when needed:
Private Sub ScrollToElement()
Dim Cadena = "<script type='text/javascript'>"
Cadena += "WindowsScrollTopAnimado( " & 1350 & "," & 1800 & ");"
Cadena += " </script>"
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "ScrollToControl", Cadena, False)
End Sub
I would insert an anchor into the HTML at the point you want to scroll to (this can be permanently in the code at the bottom, or inserted dynamically at the target point), and then redirect to yoururl.html#anchor
Unfortunately that's not very HTML5/Web2.0, the new modern way appears to be here: http://dev.w3/html5/spec/single-page.html#scroll-to-fragid
I've had some fun with this. A simple code-behind way I use is to put focus on the last element on the screen such as an empty label.
E.g.
lblMyEmptyLabel.Focus();
Of course, this doesn't smoothly scroll the window, it pretty much "teleports" you there.
Sometimes the above doesn't work (when using bootstrap modals for example) in which case the following javascript works for me:
<script type="text/javascript">
function openModal() {
document.getElementById('myElement').scrollIntoView(true);
$('#myModal').modal('show');
}
</script>
If you're using an asp element (eg a textbox) to scroll to, don't forget to add the clientidmode="static" part to the tag so the ID isn't altered on the client.