Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
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 - Controls - Using a Paged Data Source with ASP.NET and C#
Using a Paged Data Source with ASP.NET and C#


ASP.NET Controls Tutorial

This tutorial will show how we can use a Paged Data Source to create pages within a Repeater Control. C# version.

Using a GridView is great, but sometimes we need more flexibility; the flexibility and control of a repeater control. But switching from using a GridView to a Repeater can be very different. One thing is that the Repeater does not have the built-in features that the GridView has, such as the ability to create page pages based on the data. However, we can programmatically change this so that we can display pages on our repeater control, similar to how a GridView displays multiple pages of data.
First, we start off with the following:

using System.Data.SqlClient;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

In this example we are using a sample database, to which the connection string is defined in the Web.config file:

<appSettings>
<add key="ConnString" value="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True"/>
</appSettings>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

We then create our Repeater control (one for the pager, and one for the data):

<form id="form1" runat="server">
<div>
<asp:Repeater ID="repeaterPager" runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr class="text">
<td><b>Page:</b> </td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#Container.DataItem %>" CssClass="text" Runat="server">
<%# Container.DataItem %></asp:LinkButton> 
</ItemTemplate>
<FooterTemplate></td></tr></table></FooterTemplate>
</asp:Repeater>

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><table width="100%"><tr<th>ID</th><th>Name</th><th>City</th></tr></HeaderTemplate>
<ItemTemplate>
<tr><td><%#DataBinder.Eval(Container.DataItem, "theID")%></a></td>
<td><%#DataBinder.Eval(Container.DataItem, "theName")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "theCity")%></td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

And the code-behind page will look something like this:

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

public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
repeaterPager.ItemCommand +=
new RepeaterCommandEventHandler(repeaterPager_ItemCommand);
}

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

SqlDataReader myReader = cmd.ExecuteReader();
DataTable myTable = new DataTable();
myTable.Load(myReader);

PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(myTable);
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = 5; // Sets the number of records to display per page
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.PageCount > 1)
{
repeaterPager.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
repeaterPager.DataSource = pages;
repeaterPager.DataBind();
}
else
repeaterPager.Visible = false;
Repeater1.DataSource = pgitems;

Repeater1.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}

void repeaterPager_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
DisplayData();
}

}

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!





 
  Developer Resources







Server Intellect Rocks