admin管理员组

文章数量:1277258

So I need to scroll to the top of the page after an async post back in an asp update panel.

The code I used was this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestEventHandler);
function EndRequestEventHandler(sender, args)
{
    scrollTo(0,0);
}

However, I only want this to be run when I click on a certain button which causes the async postback.

How do I wire this event up in my code behind button event?

Any help would be appreacited, thanks!

So I need to scroll to the top of the page after an async post back in an asp update panel.

The code I used was this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestEventHandler);
function EndRequestEventHandler(sender, args)
{
    scrollTo(0,0);
}

However, I only want this to be run when I click on a certain button which causes the async postback.

How do I wire this event up in my code behind button event?

Any help would be appreacited, thanks!

Share Improve this question asked Nov 25, 2009 at 8:26 Jack MarchettiJack Marchetti 15.8k14 gold badges84 silver badges119 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

My quest for a solution is finally over. This question was part of the help, and the rest I found here.

Had to override ASP.NET Ajax's behaviour of memorizing the scroll position:

<script type="text/javascript"> 

var manager = Sys.WebForms.PageRequestManager.getInstance(); 

manager.add_beginRequest(beginRequest); 
function beginRequest() 
{ 
    manager._scrollPosition = null; 
} 
</script>

And then use the bit of code in the answer here, on the codebehind of the page I wanted to scroll to the top:

ScriptManager.RegisterStartupScript(this, typeof(MyControl), "someText", "window.scrollTo(0, 0)", true);

I used Farinha's answer (thanks!) and changed it slightly so I could just call the method any place I wanted to scroll to the top, but maintain the scroll position otherwise.

public static void ScrollToTop(int intPosY = 0)
    {
        string strScript = @"var manager = Sys.WebForms.PageRequestManager.getInstance(); 
            manager.add_beginRequest(beginRequest); 
            function beginRequest() 
            { 
                manager._scrollPosition = null; 
            }
            window.scroll(0," + intPosY.ToString() + ");";

        Page pagCurrent = GetCurrentPage();
        ScriptManager.RegisterStartupScript(pagCurrent, pagCurrent.GetType(), string.Empty, strScript, true);

        return;
    }

public static Page GetCurrentPage()
{
    return (HttpContext.Current.Handler as Page);
}

Improving on the answers of @Farinha and @Bradford Scott, the code can be simplified to this:

var script = 
    "Sys.WebForms.PageRequestManager.getInstance()._scrollPosition = null; " +
    "window.scrollTo(0, 0);"

ScriptManager.RegisterStartupScript(this, GetType(), "key", script, true);

I'm actually not even sure why their scripts work since they add the reset of the Sys.WebForms.PageRequestManager's _scrollPosition as an add_beginRequest handler, and here we are actually returning from the request.

Anyway, resetting the _scrollPosition right before making your own scrollTo() call definitely works.

Try this:

protected void myButon_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(MyControl), "someText", "alert('!');", true); 
}

本文标签: aspnetScroll to top of page after async post backStack Overflow