Paging in ASP.Net DataList

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 is a sealed class that provides the properties and methods to do paging and other tasks in the DataList.
Properties of  PagedDataSource
The following are (most of) the common properties of the PagedDataSource class:
  • AllowCustomPaging
  • AllowPaging
  • AllowServerPaging
  • Count
  • CurrentPageIndex
  • DataSource
  • DataSourceCount
  • FirstIndexInPage
  • IsCustomPagingEnabled
  • IsFirstPage
  • IsLastPage
  • IsPagingEnabled
  • IsReadOnly
  • IsServerPagingEnabled 
  • IsSynchronized
  • PageCount 
  • PageSize 
  • SyncRoot 
  • VirtualCount
Methods of PagedDataSource
  • 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:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "PagingInDatList" or another as you wish and specify the location.
  4. Then right-click on the Solution Explorer and select "Add New Item" - Add Web Form.
  5. Drag and drop four Buttons and a DataList control onto the <form> section of the Default.aspx page.
Now the default.aspx Page source code will look such as follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
<html xmlns="">  
<head runat="server">  
  <title>Article by Vithal Wadje</title>    
<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%">  
         <td width="10%">Id</td>  
           <td width="35%">Name</td>  
           <td width="25%"> City</td>  
             <td width="30%"> Address</td>  
      <ItemTemplate  >  
      <table width="140%">  
           <td width="10%">  
               <asp:Label  ID="Label1" runat="server" Text=&lt;%#Eval("id")%&gt;/>  
        <td width="35%">  
         <asp:Label ID="Label2" runat="server" Text=&lt;%#Eval("Name")%&gt;/>  
         <td width="25%">  
          <asp:Label ID="Label3" runat="server" Text=&lt;%#Eval("City")%&gt;/>  
         <td width="30%">  
           <asp:Label ID="Label4" runat="server" Text=&lt;%#Eval("Address")%&gt;/>  
   <table id="Paging" runat="server">  
        <asp:Button ID="Button1" runat="server" Font-Bold="true" Text="First"   
            onclick="Button1_Click" style="height: 26px"   
            <asp:Button ID="Button2" runat="server" Font-Bold="true" Text="Previous" onclick="Button2_Click"   
                <asp:Button ID="Button3" runat="server" Font-Bold="true" Text="Next" onclick="Button3_Click"   
                    <asp:Button ID="Button4" runat="server" Font-Bold="true" Text="Last" onclick="Button4_Click"   
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)  
            ViewState["PageCount"] = 0;  
        CurrentPage = (int)ViewState["PageCount"];  
    private void connection()  
        sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString;  
        con = new SqlConnection(sqlconn);  
    private void BindDatList()  
        SqlCommand com = new SqlCommand("GetEmp", con);  
        com.CommandType = CommandType.StoredProcedure;  
        SqlDataAdapter da = new SqlDataAdapter(com);  
        DataTable dt = new DataTable();  
    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;  
        ViewState["PagedDataSurce"] = dt;  
    protected void Button1_Click(object sender, EventArgs e)  
        CurrentPage = 0;  
        EmpDataList.DataSource = (DataTable)ViewState["PagedDataSurce"];  
    protected void Button2_Click(object sender, EventArgs e)  
        CurrentPage = (int)ViewState["PageCount"];  
        CurrentPage -= 1;  
        ViewState["PageCount"] = CurrentPage;  
    protected void Button3_Click(object sender, EventArgs e)  
        CurrentPage = (int)ViewState["PageCount"];  
        CurrentPage += 1;  
        ViewState["PageCount"] = CurrentPage;  
    protected void Button4_Click(object sender, EventArgs e)  
        CurrentPage = (int)ViewState["TotalCount"] - 1;  
Now run the application. The UI will look as follows:
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.
  • 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.
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

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode