As we all know DataList and Repeater both doesn't have auto paging feature like Gridview ...So to overcome this problem we have a .Net Class PagedDataSource used to do Paging in DataList and Repeater...
I'm going to implement this on Datalist ...So first Let's create a Datalist ...
<asp:DataList ID="dtlContents" runat="server" RepeatDirection="Vertical" Width="300px" DataKeyField="Id">
<ItemTemplate>
<asp:Label ID="lblContents" runat="server" Text='<%#Eval("Contents")%>'></asp:Label>
</ItemTemplate>
</asp:DataList>
For the paging create two linkbuttons and label to show the current Page Number,
<table border="0" align="right" style="width: 163px">
<tr>
<td
align="left">
<asp:LinkButton
ID="lbtnPrev" Runat="server" onclick="lbtnPrev_Click">Prev</asp:LinkButton>
</td>
<td align="center">
<asp:Label ID="lblCurrentPage" runat="server" Text=""></asp:Label>
</td>
<td align="right">
<asp:LinkButton ID="lbtnNext"
Runat="server" onclick="lbtnNext_Click">Next</asp:LinkButton>
</td>
</tr>
</table>
Now Bind the Data to Datalist...
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
}
For Previous Button....
protected void lbtnPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
}
I hope you enjoy this tutorial....
I'm going to implement this on Datalist ...So first Let's create a Datalist ...
<asp:DataList ID="dtlContents" runat="server" RepeatDirection="Vertical" Width="300px" DataKeyField="Id">
<ItemTemplate>
<asp:Label ID="lblContents" runat="server" Text='<%#Eval("Contents")%>'></asp:Label>
</ItemTemplate>
</asp:DataList>
For the paging create two linkbuttons and label to show the current Page Number,
<table border="0" align="right" style="width: 163px">
<tr>
<td
align="left">
<asp:LinkButton
ID="lbtnPrev" Runat="server" onclick="lbtnPrev_Click">Prev</asp:LinkButton>
</td>
<td align="center">
<asp:Label ID="lblCurrentPage" runat="server" Text=""></asp:Label>
</td>
<td align="right">
<asp:LinkButton ID="lbtnNext"
Runat="server" onclick="lbtnNext_Click">Next</asp:LinkButton>
</td>
</tr>
</table>
Now Bind the Data to Datalist...
public int CurrentPage///Int Variable For Holding the current page... { get { //get current page number object obj = this.ViewState["_CurrentPage"]; if (obj == null) { return 0; } else { return (int)obj; } } set { //set in viewstate the current page number this.ViewState["_CurrentPage"] = value; } }
public void BindList()
{
//Creating an object for the 'PagedDataSource' for holding the data. PagedDataSource objPage = new PagedDataSource();
try { DataSet ds = ....\\DataSource. objPage.AllowPaging = true; //Assigning the datasource to the 'objPage' object. objPage.DataSource = ds.Tables["tblData"].DefaultView; //Setting the Pagesize objPage.PageSize = 4; int count= objPage.PageCount; objPage.CurrentPageIndex = CurrentPage; //to Enable Or Disable The First and Last Button lbtnNext.Enabled = !objPage.IsLastPage; lbtnPrev.Enabled = !objPage.IsFirstPage; //Show Current Page Number lblCurrentPage.Text = "Page " + Convert.ToString(CurrentPage + 1) + " of " + Convert.ToString(objPage.PageCount);For Next Button....//Assigning Datasource to the DataList.dtlContents.DataSource = objPage; dtlContents.DataBind(); } catch(Exception ex) { throw ex; } }
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindList
();}
For Previous Button....
protected void lbtnPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindList
();}
I hope you enjoy this tutorial....