Paging Navigation with DataPager

Last post 04-19-2008 10:14 AM by Simon Deshaies. 3 replies.

Sort Posts:

  • Paging Navigation with DataPager

    03-25-2008, 11:58 PM
    • Loading...
    • imperialx
    • Joined on 06-28-2007, 10:07 AM
    • Posts 345

     Hello,

              The link navigation work's well but the Listview control doesn't move to next or previous record. Sad

     Default.aspx
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListView ID="ListView1" runat="server">
                <LayoutTemplate>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                </LayoutTemplate>
                <ItemTemplate>
                        <div class="entry">
                            <%#Eval("Field1")%>&nbsp;<%#Eval("Field2")%>
                        </div>
                </ItemTemplate>
            </asp:ListView>
            <div class="navigation">
                <asp:DataPager ID="SearchListPager" runat="server" PagedControlID="ListView1" PageSize="1">
                </asp:DataPager>
                <asp:HyperLink ID="lnkPrev" runat="server"><< Older</asp:HyperLink>
                <asp:Literal ID="Literal1" runat="server" Text=" &nbsp;&nbsp;-&nbsp; "></asp:Literal>
                <asp:HyperLink ID="lnkNext" runat="server">Newer >></asp:HyperLink>
            </div>
        </div>
        </form>
    </body>
    </html>

    Default.aspx.vb
    Partial Class _Default
        Inherits System.Web.UI.Page

        Protected Sub ListView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.PreRender
            Dim db As TableDataContext = New TableDataContext

            Try
                Dim dataTable = From b In db.Tables _
                         Select b.Field1, b.Field2

                ListView1.DataSource = dataTable
                ListView1.DataBind()

            Catch ex As Exception
                Server.Transfer("~/404.aspx")
            End Try
        End Sub

        Protected Sub ListView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DataBound
            Dim currentPage As Integer
            Dim totalPages As Integer = ListPager.TotalRowCount / ListPager.PageSize

            If Not IsNothing(Request.QueryString("Page")) Then
                currentPage = Convert.ToInt32(Request.QueryString("Page"))
            Else
                currentPage = 1
            End If

            If (totalPages <= currentPage * ListPager.PageSize) Then
                lnkNext.Enabled = False
            End If
            If (currentPage < 2) Then
                lnkPrev.Enabled = False
            End If

            Dim nextQueryString As StringBuilder = New StringBuilder
            Dim prevQueryString As StringBuilder = New StringBuilder

            Dim pageKeyExists As Boolean = False
            Dim isFirstKey As Boolean = True
            Dim key As String

            For Each key In Request.QueryString.AllKeys
                If isFirstKey <> True Then
                    nextQueryString.Append("&")
                    prevQueryString.Append("&")
                End If

                If (key = "Page") Then
                    nextQueryString.AppendFormat("Page={0}", currentPage + 1)
                    prevQueryString.AppendFormat("Page={0}", currentPage - 1)
                    pageKeyExists = True
                Else
                    nextQueryString.AppendFormat("{0}={1}", key, Request.QueryString(key))
                    prevQueryString.AppendFormat("{0}={1}", key, Request.QueryString(key))
                End If
                isFirstKey = False
            Next

            If pageKeyExists <> True Then
                If Request.QueryString.Keys.Count > 0 Then
                    nextQueryString.Append("&")
                    prevQueryString.Append("&")
                End If
                nextQueryString.Append("Page=2")
                lnkPrev.Enabled = False
            End If

            lnkPrev.NavigateUrl = "~/default.aspx?" + prevQueryString.ToString()
            lnkNext.NavigateUrl = "~/default.aspx?" + nextQueryString.ToString()

            Dim pageNo As Integer = Convert.ToInt32(currentPage)
            Dim startRowIndex As Integer = (pageNo - 1) * ListPager.PageSize
            ListPager.SetPageProperties(startRowIndex, ListPager.PageSize, True)
        End Sub

    End Class

     

    cheers,
    imperialx 

  • Re: Paging Navigation with DataPager

    04-18-2008, 9:53 AM
    • Loading...
    • Simon Deshaies
    • Joined on 11-17-2007, 3:59 PM
    • Montréal, Canada
    • Posts 58

    Hi imperialx,

    I'm having a similar problem I have to double click on the navigation links, did you come up with a solution?

    Simon Deshaies

    Support Specialist / Support C# Analyst at Nstein Technologies

    weblog.simondeshaies.net

    Please remember to click “Mark as Answer” on the post that helps you.


  • Re: Paging Navigation with DataPager

    04-18-2008, 10:44 PM
    • Loading...
    • imperialx
    • Joined on 06-28-2007, 10:07 AM
    • Posts 345

    Simon Deshaies:

    ...I have to double click on the navigation links...
     

    you mean it all work's well on your part after double clicking the links?

     

    Simon Deshaies:

    ...did you come up with a solution?

     

    I just found out that it keeps posting back so I remove the databound_event (grrr!) and put it in prerender instead then bind it afterwards.

    ...
        Protected Sub ListView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.PreRender
            Dim db As TableDataContext = New TableDataContext

            Try
                Dim dataTable = From b In db.Tables _
                         Select b.Field1, b.Field2

                --put code from DataBound here--

                ListView1.DataSource = dataTable
                ListView1.DataBind()

            Catch ex As Exception
                Server.Transfer("~/404.aspx")
            End Try
        End Sub
     ...

     

     

    cheers,
    imperialx 

     

  • Re: Paging Navigation with DataPager

    04-19-2008, 10:14 AM
    • Loading...
    • Simon Deshaies
    • Joined on 11-17-2007, 3:59 PM
    • Montréal, Canada
    • Posts 58

    Thanks for the code I'll give it a try.

    Simon Deshaies

    Support Specialist / Support C# Analyst at Nstein Technologies

    weblog.simondeshaies.net

    Please remember to click “Mark as Answer” on the post that helps you.


Page 1 of 1 (4 items)
Microsoft Communities
Page view counter