DataList中的分页无法正常工作

时间:2015-09-03 04:45:11

标签: asp.net vb.net pagination datalist

我的页面上有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

1 个答案:

答案 0 :(得分:1)

通过Paged dataSource绑定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();
            }

        }

我希望上面的例子会有所帮助..