Working With the PagedDataSource Class in ASP.NET


Introduction

The PagedDataSource class is used to provide a paging facility to a DataBound control, such as DataList, Repeater, GridView, DetailsView, etc. Although there is an AllowPaging property to handle paging in these controls. But the AllowPaging property does not work with DataList and Repeater controls. So using the PagedDataSource is very much a suitable for adding the paging feature to these (DataList and Repeater) controls. The following are some more important properties of a PagedDataSource.

AllowPaging - This property is used to enable paging. Set it to "True" or "False".

DataSource - To set datasource.

CurrentPageIndex - To set the index of current page.

PageCount - It count the total number of available page.

FirstIndexInPage - It returns the index of first page.

IsFirstPage - It returns boolean value (True or False) to indicate whether it is first page or not.

IsLastPage - It returns boolean value (True or False) to indicate whether it is last page or not.

Now, using these properties I am showing records from a database using a Repeater control. In this example, the database name is "student" and the database table is "student_detail" which has some records. I am showing all the records of the database table to make this easier to understand.



Take a ASP.NET Web Application -> take a Repeater and a button control and arrange them as in the following figure.

Code on .aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="PagedDataSourceclass.WebForm2" %> 
<!
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></title
>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
      <table>
      <asp:Repeater ID="Repeater1" runat="server">
      <HeaderTemplate>
      <h3>Details Of Students</h3>
      </HeaderTemplate>
      <ItemTemplate>
      <tr>
      <td>
      <font color="Red"><b>Roll Number</b></font>
      </td>
      <td>
    <font color="Red"><b>Name</b></font>
      </td>
      <td>
     <font color="Red"><b>Age</b></font>
      </td>
      <td>
      <font color="Red"><b>Course</b></font>
      </td>
      </tr>
       <td>
      <font color="Green"><%# Eval("roll_no") %></font>
      </td>
      <td>
    <font color="Green"><%#Eval("s_name") %></font>
      </td>
      <td>
     <font color="Green"><%#Eval("age") %></font>
      </td>
      <td>
      <font color="Green"><%#Eval("course") %></font>
      </td>
      </ItemTemplate>
        </asp:Repeater>
    <tr>
    <td colspan="4">
           <asp:Button ID="btnshow" runat="server" Height="32px" Text="Show"
            Font-Bold="true" Font-Size="12" ForeColor="DarkRed"
            Width="97px" onclick="btnshow_Click" />
    </td>
    </tr>    
  </table>
    </div>
    </form
>

</
body>
</
html>
 
aspx.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;
 

namespace
PagedDataSourceclass
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        SqlDataAdapter dadapter;
        PagedDataSource pds;
        DataSet dset;
        string connstring = "database=student;server=.;user=sa;password=wintellect";
        protected void btnshow_Click(object sender, EventArgs e)
        {
            DataBind();
        }
        public void DataBind()
        {
            dadapter=new SqlDataAdapter("select * from student_detail",connstring  );
            dset=new DataSet();
            pds=new PagedDataSource();
            dadapter.Fill(dset);
            pds.DataSource=dset.Tables[0].DefaultView;
            Repeater1.DataSource = pds;
            Repeater1.DataBind();
        }
    }
}

Run the application. After clicking the "Show" button, all records will be shown.



Now we use some properties of the PagedDatasource ( which are mentioned above ) in this application.  We add paging facility means we want to show some limited record in Repeater at a time. Take two Buttons and set its Text property to "Next" and "Previous"  and write the following code.

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;

namespace
PagedDataSourceclass
{
    public partial class WebForm2 : System.Web.UI.
Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["vs"] = 0;
            }
            position = (int)ViewState["vs"];
        }
 
        SqlDataAdapter dadapter;
        PagedDataSource pds;
        DataSet dset;
        string connstring = "database=student;server=.;user=sa;password=wintellect";
       
        protected void btnshow_Click(object sender, EventArgs e)
        {
            DataBind();
        }
        public void DataBind()
        {
            dadapter=new SqlDataAdapter("select * from student_detail",connstring  );
            dset=new DataSet();
            pds=new PagedDataSource();
            dadapter.Fill(dset);
            pds.DataSource=dset.Tables[0].DefaultView;
            pds.AllowPaging=true;
            pds.CurrentPageIndex =position;
            pds.PageSize = 2;
            Repeater1.DataSource = pds;
            Repeater1.DataBind();
        }
        int position;
        protected void btnprevious_Click(object sender, EventArgs e)
        {
            position = (int)ViewState["vs"];
            position--;
            ViewState["vs"] = position;
            DataBind();
        }
 
        protected void btnnext_Click(object sender, EventArgs e)
        {
            position = (int)ViewState["vs"];
            position++;
            ViewState["vs"] = position;
            DataBind();
        }
    }
}

 
Look at the code shown above. In the DataBind method I have set 2 for PageSize. Means only 2 records will be displayed at a time. At Button click I am setting incrementing or decrementing value for
CurrentPageIndex. Run the application and click the "Show" button.

Click at "Next" button to show next records.



If we want to disable the "Previous" button when the first page is showing and the "Next" button then when last page is showing. Update the code of DataBind method with the following code.

  public void DataBind()
        {
            dadapter=new SqlDataAdapter("select * from student_detail",connstring  );
            dset=new DataSet();
            pds=new PagedDataSource();
            dadapter.Fill(dset);
            pds.DataSource=dset.Tables[0].DefaultView;
            pds.AllowPaging=true;
            pds.CurrentPageIndex =position;
            pds.PageSize = 2;

            btnprevious.Enabled = !pds.IsFirstPage;
            btnnext.Enabled = !pds.IsLastPage;

            Repeater1.DataSource = pds;
            Repeater1.DataBind();
        }

 
Run the application and click "show" button.



Look at above figure. It is showing record of first page, so "Previous" button is disabled. Same as when you will reach at last page, the "Next" button will be disabled.

Here are some related resourse. 

Up Next
    Ebook Download
    View all
    Learn
    View all