开发者

How can i scroll to an anchor?

I have bound a datapager control to a listview.

I would like to scroll to the first item of the listview control on the DataPager click. I think this should be done with javascript. It seems that the datapager does not a开发者_高级运维llow that.

What options do I have? How can I scroll to a specific anchor when clicking on the DataPager?


you can use the basic html named anchor to scroll to a specific anchor.


You can use the javascript function scrollIntoView for that on client side or on "server side": http://www.codeproject.com/KB/aspnet/ViewControl.aspx


Thanks Tim!

And for the lazy guys out there (just like me ;), here is the VB.NET equivalent. It contains typo corrections and the new RegisterClientScriptBlock Method

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        FocusControlOnPageLoad("Label1", Me.Page)
 End Sub

 Public Sub FocusControlOnPageLoad(ByVal ClientID As String, ByVal page As System.Web.UI.Page)
        Dim csName As String = "ScrollViewScript"
        Dim csType As Type = Me.GetType
        Dim cs As ClientScriptManager = page.ClientScript
        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
            Dim csText As New StringBuilder()
            csText.Append("<script>function ScrollView(){")
            csText.Append("var el = document.getElementById('" & ClientID & "');")
            csText.Append("if (el != null){")
            csText.Append("el.scrollIntoView();")
            csText.Append("el.focus();}}")
            csText.Append("window.onload = ScrollView;")
            csText.Append("</script>")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜