views:

75

answers:

3

First of all, 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 guess by using javascript, but it seems that the datapager does not allow that...

So what option do i have? How can i scroll, onto a specific anchor, when clicking on the DataPager?

+1  A: 

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

Dror
A: 

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

Tim Schmelter
A: 

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
Chocol8