Reset scroll position after Async postback - ASP.NET

前端 未结 8 1413
花落未央
花落未央 2020-11-30 06:08

What is the best way to reset the scroll position to the top of page after the an asynchronous postback?

The asynchronous postback is initiated from a ASP.NET Grid

8条回答
  •  佛祖请我去吃肉
    2020-11-30 06:56

    taken from this tutorial:

    http://aspnet.4guysfromrolla.com/articles/111407-1.aspx

    We set MaintainScrollPositionOnPostback=true

    Then if we need to reset scroll position call the method:

    Private Sub ResetScrollPosition()
    If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(), "CreateResetScrollPosition") Then
       'Create the ResetScrollPosition() function
       ClientScript.RegisterClientScriptBlock(Me.GetType(), "CreateResetScrollPosition", _
                        "function ResetScrollPosition() {" & vbCrLf & _
                        " var scrollX = document.getElementById('__SCROLLPOSITIONX');" & vbCrLf & _
                        " var scrollY = document.getElementById('__SCROLLPOSITIONY');" & vbCrLf & _
                        " if (scrollX && scrollY) {" & vbCrLf & _
                        "    scrollX.value = 0;" & vbCrLf & _
                        "    scrollY.value = 0;" & vbCrLf & _
                        " }" & vbCrLf & _
                        "}", True)
    
       'Add the call to the ResetScrollPosition() function
       ClientScript.RegisterStartupScript(Me.GetType(), "CallResetScrollPosition", "ResetScrollPosition();", True)
    End If
    End Sub 
    

提交回复
热议问题