How To Add Adding Paging Support to the Repeater or DataList with the PagedDataSource Class

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PagingInRepeater.aspx.cs"

   Inherits="PagingInRepeater" %>

<!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>Untitled Page</title>
</head>
<body>
   <form id="frmTest" method="post" runat="server">
       <table width="100%" border="0">
           <tr>
               <td>
                   <asp:Label ID="lblCurrentPage" runat="server"></asp:Label></td>
           </tr>
           <tr>
               <td>
                   <asp:Button ID="cmdPrev" runat="server"
Text=" << " OnClick="cmdPrev_Click"></asp:Button>
                   <asp:Button ID="cmdNext" runat="server"
 Text=" >> " OnClick="cmdNext_Click"></asp:Button></td>
           </tr>
       </table>
       <table border="1">
           <asp:Repeater ID="repeaterItems" runat="server">
               <ItemTemplate>
                   <tr>
                       <td>
                           <%# DataBinder.Eval(Container.DataItem, "Id") %>
                           </b></td>
                       <td>
                           <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                           </b></td>
                       <td>
                           <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                           </b></td>
                       <td>
                           <%# DataBinder.Eval(Container.DataItem, "Address") %>
                           </b></td>
                   </tr>
               </ItemTemplate>
           </asp:Repeater>
       </table>
   </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PagingInRepeater : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           BindRepeater();
       }

   }

   public void BindRepeater()
   {

     
       if (Session["s"] != null)
       {
           DataTable dt=new DataTable();
           dt=Session["dsTemp"] as DataTable;
           PagedDataSource objPds = new PagedDataSource();
           objPds.DataSource = dt.DefaultView;
           objPds.AllowPaging = true;
           objPds.PageSize = 3;
           objPds.CurrentPageIndex = CurrentPage;
           lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
               + objPds.PageCount.ToString();
           // Disable Prev or Next buttons if necessary
           cmdPrev.Enabled = !objPds.IsFirstPage;
           cmdNext.Enabled = !objPds.IsLastPage;
           repeaterItems.DataSource = objPds;
           repeaterItems.DataBind();

       }
       else
       {

           DataTable dt = new DataTable();
           dt = GetCustomMadeDataTable();
           PagedDataSource objPds = new PagedDataSource();
           objPds.DataSource = dt.DefaultView;
           objPds.DataSource = dt.DefaultView;
           objPds.AllowPaging = true;
           objPds.PageSize = 3;
           objPds.CurrentPageIndex = CurrentPage;
           lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
               + objPds.PageCount.ToString();
           // Disable Prev or Next buttons if necessary
           cmdPrev.Enabled = !objPds.IsFirstPage;
           cmdNext.Enabled = !objPds.IsLastPage;
           repeaterItems.DataSource = objPds;
           repeaterItems.DataBind();

       }

   }
   public DataTable GetCustomMadeDataTable()
   {
       //Create a new DataTable object
       System.Data.DataTable objDataTable = new System.Data.DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Id", typeof(string));
       objDataTable.Columns.Add("FirstName", typeof(string));
       objDataTable.Columns.Add("LastName", typeof(string));
       objDataTable.Columns.Add("Address", typeof(string));
       objDataTable.Columns.Add("Email", typeof(string));
       DataRow dr;
       //Adding some data in the rows of this DataTable
       for (int i = 0; i <= 50; i++)
       {
           dr = objDataTable.NewRow();
           dr[0] = i.ToString();
           dr[1] = "FirstName" + i.ToString();
           dr[2] = "LastName" + i.ToString();
           dr[3] = "Address" + i.ToString();
           dr[4] = "Email" + i.ToString();
           objDataTable.Rows.Add(dr);

       }
       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["dtTemp"] = objDataTable;
       return objDataTable;
   }
   public int CurrentPage
   {
       get
       {
           // look for current page in ViewState
           object o = this.ViewState["_CurrentPage"];
           if (o == null)
               return 0;    // default to showing the first page
           else
               return (int)o;
       }

       set
       {
           this.ViewState["_CurrentPage"] = value;
       }
   }
   public void cmdPrev_Click(object sender, System.EventArgs e)
   {
       // Set viewstate variable to the previous page
       CurrentPage -= 1;

       // Reload control
       BindRepeater();
   }

   public void cmdNext_Click(object sender, System.EventArgs e)
   {
       // Set viewstate variable to the next page
       CurrentPage += 1;

       // Reload control
       BindRepeater();
   }
}

No comments:

Post a Comment