Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
I have done paging in Datalist using MySql database , it is working well but there is one issue i.e
if i am searching products i.e( selecting categories from dropdownlist) then it fetch all items that are related to product , then i go to second page by clicking on next button , but if i select other category to search it fetch the data in the previous page where i was searching mean on second page where i was previously.
i.e if i am on second page , then the data fetch will be on second page ,it doesn't start from first page to fetch data it remain on that page , so if some category doesn't have data , it still showing 2 page of 1 pages, and if click on next button it will show showing 3 page of 1 pages and so on .
What is this issue can anybody solve it .............
Code below to understand it .........
 

<pre lang="vb">
 
Imports MySql.Data.MySqlClient
Imports System.Data
 

  Dim cn As New MySqlConnection
    Dim cd As MySqlCommand
    Dim s, f, g, b, city As String
    Dim r, d, d1 As MySqlDataReader
    Dim da As New MySqlDataAdapter
    Dim ds As New DataSet
    Dim pg As New PagedDataSource
    Dim p As Integer
 

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ds.Clear()
        bind()
    End Sub
 
Sub bind()
cn.ConnectionString = &quot;server=localhost ; user id=root; password=; database=**&quot;
 
            cn.Open()
            s = &quot;select name,emp_add,opno,pno,eds,wl,type from main where  product=&#39;&quot; &amp; DropDownList1.SelectedItem.Text &amp; &quot;&#39; ORDER BY name &quot;
            cd = New MySqlCommand(s, cn)
            da.SelectCommand = cd
            da.Fill(ds)
            pg.DataSource = ds.Tables(0).DefaultView
            pg.AllowPaging = True
            pg.PageSize = 10
            pg.CurrentPageIndex = CurrentPage
            Label2.Text = &quot;Showing Page: &quot; &amp; (CurrentPage + 1).ToString() &amp; &quot; of &quot; &amp; pg.PageCount.ToString()
            Button2.Enabled = Not pg.IsFirstPage
            Button3.Enabled = Not pg.IsLastPage
            DataList1.DataSource = pg
            DataList1.DataBind()
            cn.Close()
            
End Sub
 

 Public Property CurrentPage() As Integer
        Get
            Dim s1 As Object = Me.ViewState("CurrentPage")
            If s1 Is Nothing Then
                Return 0
            Else
                Return CInt(s1)
            End If
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("CurrentPage") = value
        End Set
    End Property
 

 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        CurrentPage -= 1
        bind()
    End Sub
 
    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
 
        CurrentPage += 1
        bind()
    End Sub
</pre>
Posted 7-Jan-13 0:58am
Edited 7-Jan-13 16:39pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When someone changes the data in any way, you need to set 'CurrentPage' to 0, or 1, whatever the default is.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,805
1 OriginalGriff 6,696
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,792


Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100