PagedDataSource Class
Encapsulates the paging-related properties of a data-bound control (such as DataGrid, GridView, DetailsView, and FormView) that allow it to perform paging. This class cannot be inherited.
Below is the source code to demonstrate the use of PagedDataSource for implementing pagination in repeater control.
fileName : PagedDataSourceDemo.aspx
< table id="tbl1" cellspacing="0" >
< asp:Repeater ID="rptr1" runat="server" >
< ItemTemplate>
< tr >
< td " >
< asp:Label ID="lblId" runat="server" Text='<%# (Eval("ID") != null && !Eval("ID").ToString().Trim().Equals("")?Eval("ID"):"") %>'>
< /td >
< td >
< asp:Label ID="lblName" runat="server" Text='<%# (Eval("Name") != null && !Eval("Name").ToString().Trim().Equals("")?Eval("Name"):"") %>' >< /asp:Label >
< /a >
< /td >
< /tr >
< /ItemTemplate >
< /asp:Repeater >
< /table >
< table id="tbl2" cellspacing="0" >
< tr >
< td >
< asp:Repeater id="rptr2" runat="server" >
< /asp:Repeater >
< /td >
< /tr >
< /table >
< script runat="server" >
private DataTable _data = null;
private PagedDataSource _objPds;
private LinkButton _linkMore = null;
private LinkButton _linkPrevious = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["page"] != null && !Request.QueryString["page"].ToString().Equals(""))
{
CurrentPage = Int32.Parse(Request.QueryString["page"].ToString()) - 1;
LoadData();
}
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"].ToString().Trim().Equals("next"))
{
CurrentPage += 1;
}
if (Request.QueryString["action"].ToString().Trim().Equals("previous"))
{
CurrentPage -= 1;
}
LoadData();
}
if (!IsPostBack)
LoadData();
}
private void LoadData()
{
_data = ((DataTable)Cache.Get("MyData"));
if (_data != null && _data.Rows.Count > 0)
{
rptr1.DataSource = CreatePaging(_data);
rptr1.DataBind();
}
}
protected PagedDataSource CreatePaging(DataTable dt)
{
CreatePagingButton(dt);
_objPds = new PagedDataSource();
_objPds.DataSource = dt.DefaultView;
_objPds.AllowPaging = true;
_objPds.PageSize = 10;
_objPds.CurrentPageIndex = CurrentPage;
if (_linkMore != null) _linkMore.Visible = !_objPds.IsLastPage;
if (_linkPrevious != null) _linkPrevious.Visible = !_objPds.IsFirstPage;
return _objPds;
}
public int CurrentPage
{
get
{
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0;
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void CreatePagingButton(DataTable TempTable)
{
if (TempTable != null && TempTable.Rows.Count > 10)
{
if (rptr2.HasControls())
rptr2.Controls.Clear();
_linkPrevious = new LinkButton();
_linkPrevious.ID = "lnkPrevious";
_linkPrevious.Text = "Previous";
_linkPrevious.Visible = false;
_linkPrevious.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=previous";
rptr2.Controls.Add(_linkPrevious);
Label lblPrevious = new Label();
lblPrevious.ID = "lblprevious";
lblPrevious.Text = " | ";
lblPrevious.Visible = true;
rptr2.Controls.Add(lblPrevious);
int i = 0;
for (i = 0; i < TempTable.Rows.Count / 10; i++)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Articles" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
if (TempTable.Rows.Count % 10 > 0)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Page" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspxx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
_linkMore = new LinkButton();
_linkMore.ID = "lnkMore";
_linkMore.Text = "Next";
_linkMore.Visible = true;
_linkMore.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=next";
rptr2.Controls.Add(_linkMore);
}
}
< /script >
2 comments:
Hi, please kindly comment this code for a better understanding
The Paging number keep increasing. How can we limit 10 per page. then a link for Next 10 in the first page. Second Page - Previous 10 & Next 10 with 11 till 20...
Post a Comment