+ Reply to Thread
Results 1 to 4 of 4

Thread: Paging

  1. #1
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    Paging

    I'm using a Repeater control, is there a control for paging or will this be done
    manually, if it must be done manually, where the best paging script?

    Shem

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    This is what I use. It's something I came up with myself.

    .aspx
    Code:
    <asp:Repeater id="rep_message_list" OnItemDataBound="rep_message_list_onitemdatabound" runat="server">
    <itemtemplate>
    <tr valign="middle" title="View Message: <%#Eval("notice_title")%>" onmouseover="this.style.background='#F7C464'" onmouseout="this.style.background=''" style="height:15px;cursor: pointer;" onclick="window.location='messages_view.aspx?noticeref=<%#Eval("noticeref")%>'">
    <td valign="middle" class="client_data_box" style="width:35%;"><asp:HyperLink ID="hyp_sentby" runat="server" cssclass="dblue8" text='<%#Eval(HttpUtility.HtmlEncode("sentby_user"))%>' /></td>
    <td valign="middle" class="client_data_box" style="width:35%;"><asp:HyperLink ID="hyp_title" runat="server" cssclass="dblue8" text='<%#Eval(HttpUtility.HtmlEncode("notice_title"))%>' /></td>
    <td valign="middle" class="client_data_box" style="width:10%;"><asp:HyperLink ID="hyp_dateadded" runat="server" cssclass="dblue8" text='<%#format(Eval(HttpUtility.HtmlEncode("dateadded")),"dd/MM/yyyy")%>' /></td>
    <td valign="middle" class="client_data_box" style="width:20%;"><asp:HyperLink ID="hyp_read" runat="server" cssclass="dblue8" /></td>
    </tr>
    </itemtemplate>
    </asp:Repeater>
     
    <table id="frm_buttons" style="width:100%;" cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td style="width:50%;padding-left:5px;" align="left">
    <asp:ImageButton ID="frm_previous" runat="server" imageurl="form_previous_fade1.gif" ToolTip="No previous pages" Enabled="false" OnClick="but_previous" OnClientClick="window.location='#top'" /> 
    <asp:ImageButton ID="frm_first" runat="server" imageurl="form_first1.gif" ToolTip="Back to first page" visible="false" OnClick="but_first" OnClientClick="window.location='#top'" /> 
    </td>
    <td style="width:50%;padding-right:5px;" align="right">
    <asp:ImageButton ID="frm_next" runat="server" imageurl="form_next1.gif" ToolTip="Next 15 Messages" OnClick="but_next" OnClientClick="window.location='#top'" />
    </td>
    </tr>
    </table>
    
    .aspx.vb
    Code:
    Private _cur_record As Integer
     
    Sub Page_Load
           message_list_initial()
           set_buttons()
    End Sub
     
    Sub message_list_initial()
    'This function sets up the initial list that is viewed when the page is first loaded
       Dim conn As New MySqlConnection(connString)
       Dim mycommand as New MySqlCommand("SELECT field1,field2,field3 FROM tbl",conn)
       Dim rs As MySqlDataReader
     
       _cur_record = 0
     
       Try
            conn.Open()
            rs = mycommand.ExecuteReader
            rep_message_list.DataSource = rs
            rep_message_list.DataBind()
            rs.Close()
       Catch ex As Exception
            'Whatever exception dealing you want
       Finally
            mycommand.Dispose()
            conn.Close()
            conn.Dispose()
       End Try
     
    End Sub
     
    Sub but_previous(ByVal s As Object, ByVal e As ImageClickEventArgs)
           _cur_record -= 15 'I have a ddl that allows you to select how many records per page that you could put in here ddl_no_messages.SelectedValue
           if _cur_record <0 Then _cur_record = 0
           set_buttons()
           message_list_update()
    End Sub
     
    Sub but_next(ByVal s As Object, ByVal e As ImageClickEventArgs)
           _cur_record += 15 'I have a ddl that allows you to select how many records per page that you could put in here ddl_no_messages.SelectedValue
           set_buttons()
           message_list_update()
    End Sub
     
    Sub but_first(ByVal s As Object, ByVal e As ImageClickEventArgs)
          _cur_record = 0
          set_buttons()
          message_list_update()
    End Sub
     
        Sub set_buttons()
            If _cur_record > 1 Then
                frm_previous.Enabled = True
                frm_previous.ImageUrl = "form_previous1.gif"
                frm_previous.ToolTip = "Previous " & ddl_message_list_no.SelectedValue & " Messages"
            Else
                frm_previous.Enabled = False
                frm_previous.ImageUrl = "form_previous_fade1.gif"
                frm_previous.ToolTip = "No previous pages"
            End If
            If _cur_record >= (ddl_message_list_no.SelectedValue + 1) Then
                frm_first.Visible = True
            Else
                frm_first.Visible = False
            End If
            If (no_messages - ddl_message_list_no.SelectedValue) > _cur_record Then
                frm_next.Enabled = True
                frm_next.ImageUrl = "form_next1.gif"
                frm_next.ToolTip = "Next " & ddl_message_list_no.SelectedValue & " Messages"
            Else
                frm_next.Enabled = False
                frm_next.ImageUrl = "form_next_fade1.gif"
                frm_next.ToolTip = "No more pages"
            End If
        End Sub
     
    Sub message_list_update()
            Dim conn As New MySqlConnection(connString)
            Dim mycommand As New MySqlCommand
            Dim rs As MySqlDataReader
            mycommand.Connection = conn
    'You could substitute 15 in the SQL for the ddl_no_messages.SelectedValue
            Dim strsql As String = "SELECT field1,field2,field3 FROM tbl LIMIT " & _cur_record & ",15;"
     
       Try
            conn.Open()
            mycommand.CommandText = strsql
            rs = mycommand.ExecuteReader()
            rep_message_list.DataSource = rs
            rep_message_list.DataBind()
            rs.Close()
       Catch ex As Exception
            'Whatever exception dealing you want
       Finally
            mycommand.Dispose()
            conn.Close()
            conn.Dispose()
       End Try
     
    End Sub
     
        Private ReadOnly Property no_messages() As Integer
            Get
                Dim conn As New MySqlConnection(connString)
                Dim strsql As String = "SELECT COUNT(field1) AS no_records FROM tbl"
                Dim mycommand As New MySqlCommand
                mycommand.Connection = conn
                Dim rs As MySqlDataReader
                Try
                    conn.Open()
                    mycommand.CommandText = strsql
                    rs = mycommand.ExecuteReader()
                    If rs.HasRows Then
                        rs.Read()
                        Return rs("no_records")
                    Else
                        Return 0
                    End If
                    rs.Close()
                Catch ex As Exception
                    'Whatever exception dealing you want
                    Return 0
                Finally
                    mycommand.Dispose()
                    conn.Close()
                    conn.Dispose()
                End Try
            End Get
        End Property
    
    So, basically, I have a sub to deal with each button; previous, next and first. A sub to build the initial list of items. A property to count the total items in the list, a sub to deal with which buttons need to be enabled, showing etc. and a sub to deal with updating the list based on what the current record to start at is.

    Hope that helps...

  3. #3
    Barn Newbie yangski is an unknown quantity at this point yangski's Avatar
    Join Date
    Jul 2009
    Posts
    26
    Rep Power
    3

    do you have any code that works in C#? I also need this paging. I just set the "Allow Paging" of my GridView in the properties window so every time I click the next page, there's an error showing up.

    Please help.

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO