使用Mysql数据库在Datalist中进行分页 [英] Paging in Datalist using Mysql Database

查看:103
本文介绍了使用Mysql数据库在Datalist中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySql数据库在Datalist中进行了分页,它运行良好但是有一个问题,例如

如果我正在搜索产品即(从下拉列表中选择类别)然后它获取所有项目与产品相关,然后我通过点击下一个按钮进入第二页,但是如果我选择其他类别进行搜索,则在上一页中获取数据,我在那里搜索的是我之前在第二页上的数据。

即如果我在第二页,那么数据提取将在第二页上,它不会从第一页开始获取它保留在该页面上的数据,所以如果某个类别没有数据,它仍然显示2页1页,如果点击下一个按钮,它将显示3页1页,依此类推。

这个问题可以解决任何问题.............

以下代码了解它........ 。





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>

推荐答案

当有人以任何方式更改数据时,您需要设置' 'currentPage''为0或1,无论默认值是什么。
When someone changes the data in any way, you need to set ''CurrentPage'' to 0, or 1, whatever the default is.


这篇关于使用Mysql数据库在Datalist中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆