DataList 中的分页不起作用

Pagination in DataList not working

我的页面上有 DataList 用于显示来自数据库的记录。我已经按照 this 教程使用 DataList 实现分页。

它在第一页显示 5 条记录,但在下一页,Datalist 加载为空。我可以在单击下一个和上一个按钮时看到当前页面更改的值。

以下是我的aspx页面代码:

<asp:DataList ID="DataList1" runat="server">
    <HeaderTemplate>
        <table class="TicketTableInner">
             <tr>
                 <td class="ticketlistheaderrow" align="left" valign="middle" width="8%">Ticket ID</td>
                 <td class="ticketlistheaderrow" align="left" valign="middle" width="21%">Category</td>
                 <td class="ticketlistheaderrow" align="left" valign="middle" width="41%">Problem</td>
                 <td class="ticketlistheaderrow" align="left" valign="middle" width="15%">Create Date</td>
                 <td class="ticketlistheaderrow" align="left" valign="middle" width="15%">Reply Date</td>
              </tr>
    </HeaderTemplate>
    <ItemTemplate>
              <tr class="TicketRow<%#Eval("IsReadString")%>"  onclick="UpdateTicketID('<%#Eval("TicketIDD")%>')">
                 <td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#Eval("TicketIDFROMDB")%></td>
                 <td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#TrimLenght(Eval("Category"), 30)%></td>
                 <td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#TrimLenght(Eval("Problem"), 70)%></td>
                 <td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#Eval("CreatedDate")%></td>
                 <td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%# ProcessMyDataItemForNA(Eval("RepliedDate"))%></td>
             </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
</asp:DataList>
   <table class="style1">
            <tr>
            <td class="style2">
                <asp:LinkButton ID="LinkButton2" runat="server"
                    OnClick="LinkButton2_Click">Next</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton3" runat="server"
                    OnClick="LinkButton3_Click">Previous</asp:LinkButton>
            </td>
        </tr>
   </table>

下面是代码背后的代码:

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class supportTickets_myTickets
    Inherits System.Web.UI.Page
    Public TicketStatus As String = ""
    Public TicketID As String = ""
    Dim pg As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CurrentPageIndex = 0
            showData()
        Else
        End If
    End Sub

    Private Function GetDataTable() As DataTable
       '//This function returns record from database in the form of dataTable
    End Function


    Private Sub showData()
        Dim pgd As New PagedDataSource()
        Dim ds As DataSet = GetDataSet()
        pgd.DataSource = ds.Tables(0).DefaultView
        pgd.AllowPaging = True
        pgd.PageSize = 5
        pgd.CurrentPageIndex = CurrentPageIndex

        LinkButton2.Enabled = Not (pgd.IsLastPage)
        LinkButton3.Enabled = Not (pgd.IsFirstPage)

        DataList1.DataSource = pgd
        DataList1.DataBind()
    End Sub


    Public Property CurrentPageIndex() As Integer
        Get
            If ViewState("pg") Is Nothing Then
                Return 0
            Else
                Return Convert.ToInt16(ViewState("pg"))
            End If
        End Get
        Set(value As Integer)
            ViewState("pg") = value
        End Set
    End Property


    Protected Sub LinkButton2_Click(sender As Object, e As EventArgs)
        CurrentPageIndex += 1
        showData()
    End Sub
    Protected Sub LinkButton3_Click(sender As Object, e As EventArgs)
        CurrentPageIndex -= 1
        showData()
    End Sub

End Class

通过分页数据源绑定DataList

 public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                    return Convert.ToInt32(ViewState["PageNumber"]);
                else
                    return 0;
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }



private void BindListing()
        {
            DataTable dt = Method which return datatable;
            if (dt.Rows.Count <= 0)
            {
                lnknext.Visible = false;
                lnkPrevious.Visible = false;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Error", "alert('No records found!!!.');", true);
                return;
            }
            PagedDataSource pgitems = new PagedDataSource();
            DataView dv = new DataView(dt);
            pgitems.DataSource = dv;
            pgitems.AllowPaging = true;
            pgitems.PageSize = 10;
            pgitems.CurrentPageIndex = PageNumber;

            DataList1.DataSource = pgitems;
            DataList1.DataBind();

        }



protected void lnkPrevious_Click(object sender, EventArgs e)
        {
            PageNumber -= 1;
            BindListing();
        }
        protected void lnknext_Click(object sender, EventArgs e)
        {

            PageNumber += 1;
            BindListing();
        }



protected void Page_Load(object sender, EventArgs e)
        {       
            if (!IsPostBack)
            {
                BindListing();
            }

        }

希望上面的例子能帮到你..