I have often read the common question in forum posts of how to do paging in a DataList so by considering those requirements I have decided to write this article. In a DataList there is no direct method to do paging as in a GridView. To do paging in a DataList the PagedDataSource class provides the properties and methods to do the paging in the datalist. So let us start from the defination of PagedDataSource so beginners can also understand.
PagedDataSource
PagedDataSource is a sealed class that provides the properties and methods to do paging and other tasks in the DataList.
Properties of PagedDataSource
- AllowCustomPaging
- AllowPaging
- AllowServerPaging
- Count
- CurrentPageIndex
- DataSource
- DataSourceCount
- FirstIndexInPage
- IsCustomPagingEnabled
- IsFirstPage
- IsLastPage
- IsPagingEnabled
- IsReadOnly
- IsServerPagingEnabled
- IsSynchronized
- PageCount
- PageSize
- SyncRoot
- VirtualCount
- CopyTo
- GetEnumerator
- GetItemProperties
- GetListName
I hope you have learned about the PagedDataSource. Now let us see the practice. Suppose we have an Employee table with the following records:
Now Let us create the sample web application as follows:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "PagingInDatList" or another as you wish and specify the location.
- Then right-click on the Solution Explorer and select "Add New Item" - Add Web Form.
- Drag and drop four Buttons and a DataList control onto the <form> section of the Default.aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Article by Vithal Wadje</title> </head> <body bgcolor="navy"> <form id="form1" runat="server"> <br /> <br /> <br /> <div style="color:white"> <asp:DataList ID="EmpDataList" runat="server"> <HeaderTemplate > <table width="140%"> <tr> <td width="10%">Id</td> <td width="35%">Name</td> <td width="25%"> City</td> <td width="30%"> Address</td> </tr> </table> </HeaderTemplate> <ItemTemplate > <table width="140%"> <tr> <td width="10%"> <asp:Label ID="Label1" runat="server" Text=<%#Eval("id")%>/> </td> <td width="35%"> <asp:Label ID="Label2" runat="server" Text=<%#Eval("Name")%>/> </td> <td width="25%"> <asp:Label ID="Label3" runat="server" Text=<%#Eval("City")%>/> </td> <td width="30%"> <asp:Label ID="Label4" runat="server" Text=<%#Eval("Address")%>/> </td> </tr> </table> </ItemTemplate> </asp:DataList> <table id="Paging" runat="server"> <tr> <td> <asp:Button ID="Button1" runat="server" Font-Bold="true" Text="First" onclick="Button1_Click" style="height: 26px" /></td> <td> <asp:Button ID="Button2" runat="server" Font-Bold="true" Text="Previous" onclick="Button2_Click" /></td> <td> <asp:Button ID="Button3" runat="server" Font-Bold="true" Text="Next" onclick="Button3_Click" /></td> <td> <asp:Button ID="Button4" runat="server" Font-Bold="true" Text="Last" onclick="Button4_Click" /></td> </tr> </table> </div> </form> </body> </html>
The entire Default.aspx code will look as follows.
default.aspx.cs code
using System; using System.Data.SqlClient; using System.Configuration; using System.Data; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { SqlConnection con; int CurrentPage; string sqlconn; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDatList(); ViewState["PageCount"] = 0; } CurrentPage = (int)ViewState["PageCount"]; } private void connection() { sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString; con = new SqlConnection(sqlconn); } private void BindDatList() { connection(); SqlCommand com = new SqlCommand("GetEmp", con); com.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(com); DataTable dt = new DataTable(); con.Open(); da.Fill(dt); con.Close(); DataListPaging(dt); } private void DataListPaging(DataTable dt) { //creating object of PagedDataSource; PagedDataSource PD = new PagedDataSource(); PD.DataSource = dt.DefaultView; PD.PageSize = 2; PD.AllowPaging = true; PD.CurrentPageIndex = CurrentPage; Button1.Enabled = !PD.IsFirstPage; Button2.Enabled = !PD.IsFirstPage; Button4.Enabled = !PD.IsLastPage; Button3.Enabled = !PD.IsLastPage; ViewState["TotalCount"] = PD.PageCount; EmpDataList.DataSource = PD; EmpDataList.DataBind(); ViewState["PagedDataSurce"] = dt; } protected void Button1_Click(object sender, EventArgs e) { CurrentPage = 0; EmpDataList.DataSource = (DataTable)ViewState["PagedDataSurce"]; EmpDataList.DataBind(); } protected void Button2_Click(object sender, EventArgs e) { CurrentPage = (int)ViewState["PageCount"]; CurrentPage -= 1; ViewState["PageCount"] = CurrentPage; DataListPaging((DataTable)ViewState["PagedDataSurce"]); } protected void Button3_Click(object sender, EventArgs e) { CurrentPage = (int)ViewState["PageCount"]; CurrentPage += 1; ViewState["PageCount"] = CurrentPage; DataListPaging((DataTable)ViewState["PagedDataSurce"]); } protected void Button4_Click(object sender, EventArgs e) { CurrentPage = (int)ViewState["TotalCount"] - 1; DataListPaging((DataTable)ViewState["PagedDataSurce"]); } }
Now in the preceding you see that the first and previous buttons are disabled because the page is loaded the first time. Now click on the Next button and it will show you the next records as in the following:
Now click on the Previous button. It will show you Previous records as in the following:
Now click on the Last button. It will show you the last records as in the following:
Now you have seen how to do paging in a DataList.
Notes
- For detailed code please download the sample Zip file.
- Do a proper validation such as date input values when implementing.
- Make the changes in the web.config file depending on your server details for the connection string.
Summary
From all the preceding examples you have learned how to do paging in a DataList. I hope this article is useful for all readers, if you have a suggestion then please contact me.
Post a Comment