Search content within the blog

Thursday, November 13, 2008

Adding Paging Support to the Repeater or DataList with the PagedDataSource Class

Original Reference: http://aspnet.4guysfromrolla.com/articles/081804-1.aspx
prerequisite's:
  • Create a XML File called Items.XML And Paste the contents present in 'XMLFile Contents' Section.
  • Paste the Contents present in 'ASPX page content' Section into ASPX Page.
  • Paste the Contents present in 'ASPX.CS Content' Section into ASPX .CS Page.
XMLFile Contents

<?xml version="1.0" encoding="utf-8" ?>
<Items>
<Item>
<ItemName>Item1</ItemName>
<ItemDescription>Item1 Description</ItemDescription>
<ItemPrice>1.50</ItemPrice>
<ItemInStock>Y</ItemInStock>
</Item>
<Item>
<ItemName>Item2</ItemName>
<ItemDescription>Item2 Description</ItemDescription>
<ItemPrice>2.50</ItemPrice>
<ItemInStock>Y</ItemInStock>
</Item>

</Item>
</Items>


ASPX page content

<h3>
paging Repeater content using PageDatasourse</h3>
<asp:Button runat="server" ID="btnXMLReader" Text="Fill Repeater" OnClick="btnXMLReader_Click" />
<table width="100%" border="0">
<tr>
<td>
Repeater control with Paging functionality</td>
</tr>
<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">
<tr>
<td width="60%">
<asp:Repeater runat="server" ID="rptDataFromXML">
<HeaderTemplate >
<table width="100%">
<tr>
<td width="50%" align="center">Data From XML File With Paging Functionality.</td>
<td width="50%" align="center">Page Size=2</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td align="left" style="color:Blue" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemName")%></td>
<td align="left" style="color:Red" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemDescription")%></td>
<td align="left" style="color:Chocolate" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemInStock")%></td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td align="left" style="color:Chocolate" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemName")%></td>
<td align="left" style="color:Blue" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemDescription")%></td>
<td align="left" style="color:Red" width="20%"><%# DataBinder.Eval(Container.DataItem, "ItemInStock")%></td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
<h3>
End of paging Repeater content using PageDatasourse</h3>

ASPX.CS Content

protected void btnXMLReader_Click(object Sender, EventArgs e)
{

#region --Reading data into dataset--
DataSet dsXmlContent = new DataSet();
dsXmlContent.ReadXml(Server.MapPath("XMLFiles/Items.xml"), XmlReadMode.Auto);

#endregion

#region --Bind the data obtained--

objDataSource.PageSize = 2;
objDataSource.AllowPaging = true;
objDataSource.DataSource = dsXmlContent.Tables[0].DefaultView;
objDataSource.CurrentPageIndex = CurrentPage;
if (dsXmlContent.Tables[0].Rows.Count > 0)
{
rptDataFromXML.DataSource = objDataSource;
rptDataFromXML.DataBind();
cmdPrev.Enabled = !(objDataSource.IsFirstPage);
cmdNext.Enabled = !(objDataSource.IsLastPage);
}
else
{
lblCurrentPage.Text = "No Records avaliable";
lblCurrentPage.ForeColor = System.Drawing.Color.Red;
}
#endregion

}
public int CurrentPage
{
get
{
object objTemp = this.ViewState["_currentPage"];
if (objTemp == null)
{
return 0;
}
else
{
return((int)objTemp);
}
}
set
{
this.ViewState["_currentPage"] = value;
}
}
protected void cmdPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
btnXMLReader_Click(sender, e);
}
protected void cmdNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
btnXMLReader_Click(sender, e);
}

No comments:

Post a Comment