Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Website Navigation - Using QueryString for Pager on Repeater Control in C#
Using QueryString for Pager on Repeater Control in C#


ASP.NET Website Navigation Tutorial

Tutorial shows how to add page links to your Repeater control and make use of QueryString to move between pages. C# version.

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;

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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();
}
}

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

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks