DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 How to page Repeater using ASP.NET 2.0 and C#.NET

This tutorial will show you how to page Repeater using ASP.NET 2.0 and C#.NET .First, you need to import the System.Data.SqlClient namespace. This tutorial will show you how to page Repeater using ASP.NET 2.0 and C#.NET .First, you  need to import the System.Data.SqlClient namespace.
using System.Data.SqlClient;

We use the Page_Load event to display data. And use the btnStart_Click event to display the first page. We use the btnBack_Click event to  display the previous page, and use the btnGo_Click event to display the next page,use the btnEnd_Click event to display the last page.

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.

The Repeater is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all HTML layout, formatting, and style tags within the control's templates.The Repeater is the only control that allows the developers to split HTML tags across the templates. To create a table using templates, include the begin table tag (<table>) in the HeaderTemplate, a single table row tag (<tr>) in the ItemTemplate, and the end table tag (</table>) in the  FooterTemplate.The Repeater has no built-in selection or editing support. The user may use the ItemCommand event to process control events that are raised from the templates to the control.A Repeater binds its ItemTemplate and AlternatingItemTemplate to a data model declared and referenced by its DataSource property. The HeaderTemplate, FooterTemplate, and SeparatorTemplate are not data-bound.If the data source of the Repeater is set but no data is returned, the control renders the HeaderTemplate and FooterTemplate with no items. If the data source is a null reference (Nothing in Visual Basic), the Repeater is not rendered.

public partial class RepeaterPageSetting : System.Web.UI.Page
{
SqlConnection scon=new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa");
SqlDataAdapter sDA;
DataSet ds;
int currentPage;
int maxPage;
const int rowCount = 3;
int rowSum;

private void BindData()
{
Repeater1.DataSource = ds;
Repeater1.DataBind();
lblIndex.Text = currentPage.ToString();
}

private void readpage(int n)
{
sDA = new SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon);
ds = new DataSet();
ds.Clear();
sDA.Fill(ds, (n - 1) * rowCount,rowCount, "employees");
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
sDA = new SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon);
ds = new DataSet();
try
{
sDA.Fill(ds, "employees");
rowSum = ds.Tables[0].Rows.Count;
}
catch (Exception ex)
{
rowSum = 0;
return;
}
if (rowSum % rowCount > 0)
{
maxPage = rowSum / rowCount + 1;
}
else
{
maxPage = rowSum / rowCount;
}
currentPage = 1;
readpage(currentPage);
BindData();
lblTotal.Text = maxPage.ToString();
btnStart.Enabled = false;
btnBack.Enabled = false;
}
}

protected void btnStart_Click(object sender, EventArgs e)
{
currentPage = 1;
readpage(currentPage);
BindData();
btnStart.Enabled = false;
btnBack.Enabled = false;
btnGo.Enabled = true;
btnEnd.Enabled = true;
}

protected void btnBack_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(lblIndex.Text) > 2)
{
btnGo.Enabled = true;
btnEnd.Enabled = true;
}
else
{
btnStart.Enabled = false;
btnBack.Enabled = false;
btnGo.Enabled = true;
btnEnd.Enabled = true;
}
currentPage = Convert.ToInt32(lblIndex.Text) - 1;
readpage(currentPage);
BindData();
}

protected void btnGo_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(lblIndex.Text) < Convert.ToInt32(lblTotal.Text) - 1)
{
btnStart.Enabled = true;
btnBack.Enabled = true;
}
else
{
btnStart.Enabled = true;
btnBack.Enabled = true;
btnGo.Enabled = false;
btnEnd.Enabled = false;
}
currentPage = Convert.ToInt32(lblIndex.Text) + 1;
readpage(currentPage);
BindData();
}

protected void btnEnd_Click(object sender, EventArgs e)
{
currentPage = Convert.ToInt32(lblTotal.Text);
readpage(currentPage);
BindData();
btnStart.Enabled = true;
btnBack.Enabled = true;
btnGo.Enabled = false;
btnEnd.Enabled = false;
}
}

The front end Default.aspx page looks something like this:

<table id="tbl1">
<tr>
<td align="center">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr align="left">
<td align="left"><%#DataBinder.Eval(Container,"DataItem.employeeid")%></td>
<td align="left"><%#DataBinder.Eval(Container,"DataItem.lastname")%></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td align="center">
This is Number
<asp:Label ID="lblIndex" runat="server"></asp:Label>; Total is
<asp:Label ID="lblTotal" runat="server"></asp:Label></td>
</tr>
<tr>
<td align="center" style="height: 26px">
<asp:Button ID="btnStart" runat="server" Text="frist page" OnClick="btnStart_Click" />
<asp:Button ID="btnBack" runat="server" Text="previous page" OnClick="btnBack_Click" />
<asp:Button ID="btnGo" runat="server" Text="next page" OnClick="btnGo_Click" />
<asp:Button ID="btnEnd" runat="server" Text="last page" OnClick="btnEnd_Click" /></td>
</tr>
</table>

The flow for the code behind page is as follows.

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 RepeaterPageSetting : System.Web.UI.Page
{
SqlConnection scon=new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa");
SqlDataAdapter sDA;
DataSet ds;
int currentPage;
int maxPage;
const int rowCount = 3;
int rowSum;

private void BindData()
{
Repeater1.DataSource = ds;
Repeater1.DataBind();
lblIndex.Text = currentPage.ToString();
}

private void readpage(int n)
{
sDA = new SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon);
ds = new DataSet();
ds.Clear();
sDA.Fill(ds, (n - 1) * rowCount,rowCount, "employees");
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
sDA = new SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon);
ds = new DataSet();
try
{
sDA.Fill(ds, "employees");
rowSum = ds.Tables[0].Rows.Count;
}
catch (Exception ex)
{
rowSum = 0;
return;
}
if (rowSum % rowCount > 0)
{
maxPage = rowSum / rowCount + 1;
}
else
{
maxPage = rowSum / rowCount;
}
currentPage = 1;
readpage(currentPage);
BindData();
lblTotal.Text = maxPage.ToString();
btnStart.Enabled = false;
btnBack.Enabled = false;
}
}

protected void btnStart_Click(object sender, EventArgs e)
{
currentPage = 1;
readpage(currentPage);
BindData();
btnStart.Enabled = false;
btnBack.Enabled = false;
btnGo.Enabled = true;
btnEnd.Enabled = true;
}

protected void btnBack_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(lblIndex.Text) > 2)
{
btnGo.Enabled = true;
btnEnd.Enabled = true;
}
else
{
btnStart.Enabled = false;
btnBack.Enabled = false;
btnGo.Enabled = true;
btnEnd.Enabled = true;
}
currentPage = Convert.ToInt32(lblIndex.Text) - 1;
readpage(currentPage);
BindData();
}

protected void btnGo_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(lblIndex.Text) < Convert.ToInt32(lblTotal.Text) - 1)
{
btnStart.Enabled = true;
btnBack.Enabled = true;
}
else
{
btnStart.Enabled = true;
btnBack.Enabled = true;
btnGo.Enabled = false;
btnEnd.Enabled = false;
}
currentPage = Convert.ToInt32(lblIndex.Text) + 1;
readpage(currentPage);
BindData();
}

protected void btnEnd_Click(object sender, EventArgs e)
{
currentPage = Convert.ToInt32(lblTotal.Text);
readpage(currentPage);
BindData();
btnStart.Enabled = true;
btnBack.Enabled = true;
btnGo.Enabled = false;
btnEnd.Enabled = false;
}
}

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




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!