Navigator: Home - Advanced - Implementing a Pager with the Repeater Control in C#

Implementing a Pager with the Repeater Control in C#

This tutorial will show how we can add a pager to a repeater control, and use a querystring value to move between the pages. This method is much more SEO-friendly than using postback, like the built-in GridView pager. C# version.

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

The Repeater control is one of the most powerful controls in the ASP.NET toolbox. However, there is no built-in method of creating pages to display with it, like the GridView, for example. Building a pager for the Repeater control is possible in more than one ways, but if we use PostBack, then the result is not really SEO-friendly.

An alternative to using PostBack is to use a QueryString for the page numbers. This tutorial will show how we can do this, based upon data in the Repeater.
First, add the following assembly reference:

using System.Data.SqlClient;

For this example, we use a sample database. Add the connection string in the Web.Config similar to the following:

<appSettings>
<add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" />
</appSettings>

Next, we add the following to the ASPX page (a Repeater) and a Label:

<form id="form1" runat="server">
<div>
<asp:Label ID="lblCurrpage" runat="server"></asp:Label>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><table width="100%"><tr><th>Name</th><th>City</th></tr></HeaderTemplate>
<ItemTemplate>
<tr><td><%#DataBinder.Eval(Container.DataItem, "name")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "city")%></td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>

The code-behind should look something like this:

using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DisplayData();
}

public void DisplayData()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand("SELECT * FROM [Table1]", myConnection);
cmd.Connection.Open();

SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
myDA.Fill(ds, "Table1");
PagedDataSource pageds = new PagedDataSource();
pageds.DataSource = ds.Tables["Table1"].DefaultView;
pageds.AllowPaging = true;
pageds.PageSize = 3;

int curpage = 0;

if (Request.QueryString["page"] != null)
{
curpage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
curpage = 1;
}

pageds.CurrentPageIndex = curpage - 1;

if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
lblCurrpage.Text = "Pages: 1";
else if (pageds.DataSourceCount == 0)
lblCurrpage.Text = "No data to display.";
else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
lblCurrpage.Text = "Pages: <a href='Default.aspx?page=1'>1</a>";
for (int i = 2; i <= pageds.PageCount; i++)
{
if (i == curpage)
lblCurrpage.Text = lblCurrpage.Text + ", " + i.ToString();
else
lblCurrpage.Text = lblCurrpage.Text + ", <a href='Default.aspx?page=" + i.ToString() + "'>" + i.ToString() + "</a>";
}

Repeater1.DataSource = pageds;
Repeater1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

411asp.net123aspxDotNetFreaksServer Intellect