[RESOLVED]set vertical scrollposition to top from code behind how please?

Hello everyone, I have a label and I want to display error message when there is an error. So I want to set the vertical scroll position from code behind on a button click event. Something like this:

Label1.text="There is an error"

‘Javascript to scroll to top comes here

So that the user can see the error message. Is it possible to call javascript from code behind? 



This post may help to you:



Good luck

You can use ClientScript.RegisterStartupScript() to register "window.scrollTo(0, 0);"

Hello MetalAsp.net, please help me a bit more. where should I implement your suggestion? Is it page load event or is it after I Label1.text? How can I register window.scrollTo(0,0);?



The simplest way to execute JavaScrit without any special methods and
without writing in source  of.asps page is to include somewhere in your .aspx
page one Label control (in example having id=codeLabel) and to include in
 Page_Load event procedure the following code:

protected void Page_Load(object sender, EventArgs e)

O. Zhelezov

I’m sorry, this need one correction – script block

protected void Page_Load(object sender, EventArgs e)

O. Zhelezov

Zhelezov, after include the text to CodeLabel, how can i call it after the Label1.text is displayed? The important point is I put the Label1 on some where near the top of the page, so when Label1.text has some text, the page will scroll to top. Something
like this:

Label1.text="there is an error"

‘how can i call the script in the codelabel over here?





You could create one JS in the browser client ,

<script type="text/javascript">

        function test()

        { window.scrollTo(0, 0); }             </script>

and  in the code behind you can do like this

protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = "there is  an error";
        ClientScriptManager cs = this.ClientScript;
        cs.RegisterStartupScript(this.GetType(), "test", "test()",true);



Hope it helps.




Try this:

Add following code in aspx:

<asp:HyperLink ID="LnkFocus" runat="server"></asp:HyperLink>

And add this in cs after postback:

Thanks for all the replies. Just had a chance to do some more research. I have tried the suggestion above but did not work for me. But I found this link which is working:


function resetDotNetScrollPosition()
var scrollX = document.getElementById(‘__SCROLLPOSITIONX’);
var scrollY = document.getElementById(‘__SCROLLPOSITIONY’);

if(scrollX != null && scrollY != null)
scrollX.value = 0;
scrollY.value = 0;

Page.ClientScript.RegisterStartupScript(Page.Clien tScript.GetType(), Page.ClientID, "resetDotNetScrollPosition();", true);

Hope it helps others too.


Leave a Reply