Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in DataList not working

I have DataList on my page for showing records coming from database. I have followed this tutorial for implementing pagination with DataList.

It shows 5 record on first page but on next page, Datalist loads empty. I can see the value of current page changes on Clicking Next and Previous buttons.

Following is my code of aspx page :

<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>

And following is code from code behind :

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
like image 826
Anjum Avatar asked Nov 09 '22 05:11

Anjum


1 Answers

Bind DataList by Paged dataSource

 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();
            }

        }

I hope above example will help out..

like image 192
Sunil.Kumar.Singh Avatar answered Nov 14 '22 21:11

Sunil.Kumar.Singh