我的页面上有DataList,用于显示来自数据库的记录。我已遵循this教程来实现与DataList的分页。

它在首页上显示5条记录,但是在下一页上,数据列表加载为空。单击“下一个”和“上一个”按钮,可以看到当前页面更改的值。

以下是我的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

最佳答案

通过分页数据源绑定(bind)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();
            }

        }

希望以上示例对您有所帮助。

关于asp.net - DataList中的分页不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32367125/

10-11 11:27