DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 How to page Repeater using ASP.NET 2.0 and VB.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.
Imports 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.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.

Partial Class RepeaterPageSettingVB
Inherits System.Web.UI.Page

Dim scon As New SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa")
Dim sDA As SqlDataAdapter
Dim ds As New Data.DataSet
Dim currentPage As Integer
Dim maxPage As Integer
Const rowCount As Integer = 3
Dim rowSum As Integer

Sub BindData()
Repeater1.DataSource = ds
Repeater1.DataBind()
lblIndex.Text = currentPage
End Sub

Sub readpage(ByVal n As Integer)
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New Data.DataSet
ds.Clear()
sDA.Fill(ds, (n - 1) * rowCount, rowCount, "employees")
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New Data.DataSet
Try
sDA.Fill(ds, "employees")
rowSum = ds.Tables(0).Rows.Count
Catch ex As Exception
rowSum = 0
End Try
If rowSum = 0 Then Exit Sub
If rowSum Mod rowCount > 0 Then
maxPage = rowSum \ rowCount + 1
Else
maxPage = rowSum \ rowCount
End If
currentPage = 1
readpage(currentPage)
BindData()
lblTotal.Text = maxPage
btnStart.Enabled = False
btnBack.Enabled = False
End If
End Sub

Protected Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
currentPage = 1
readpage(currentPage)
BindData()
btnStart.Enabled = False
btnBack.Enabled = False
btnGo.Enabled = True
btnEnd.Enabled = True
End Sub

Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
If lblIndex.Text > 2 Then
btnGo.Enabled = True
btnEnd.Enabled = True
Else
btnStart.Enabled = False
btnBack.Enabled = False
btnGo.Enabled = True
btnEnd.Enabled = True
End If
currentPage = lblIndex.Text - 1
readpage(currentPage)
BindData()
End Sub

Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
If lblIndex.Text < lblTotal.Text - 1 Then
btnStart.Enabled = True
btnBack.Enabled = True
Else
btnStart.Enabled = True
btnBack.Enabled = True
btnGo.Enabled = False
btnEnd.Enabled = False
End If
currentPage = lblIndex.Text + 1
readpage(currentPage)
BindData()
End Sub

Protected Sub btnEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnd.Click
currentPage = lblTotal.Text
readpage(currentPage)
BindData()
btnStart.Enabled = True
btnBack.Enabled = True
btnGo.Enabled = False
btnEnd.Enabled = False
End Sub
End Class

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.

Imports System.Data.SqlClient

Partial Class RepeaterPageSettingVB
Inherits System.Web.UI.Page

Dim scon As New SqlConnection("server=localhost;database=northwind;uid=sa;pwd=sa")
Dim sDA As SqlDataAdapter
Dim ds As New Data.DataSet
Dim currentPage As Integer
Dim maxPage As Integer
Const rowCount As Integer = 3
Dim rowSum As Integer

Sub BindData()
Repeater1.DataSource = ds
Repeater1.DataBind()
lblIndex.Text = currentPage
End Sub

Sub readpage(ByVal n As Integer)
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New Data.DataSet
ds.Clear()
sDA.Fill(ds, (n - 1) * rowCount, rowCount, "employees")
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New Data.DataSet
Try
sDA.Fill(ds, "employees")
rowSum = ds.Tables(0).Rows.Count
Catch ex As Exception
rowSum = 0
End Try
If rowSum = 0 Then Exit Sub
If rowSum Mod rowCount > 0 Then
maxPage = rowSum \ rowCount + 1
Else
maxPage = rowSum \ rowCount
End If
currentPage = 1
readpage(currentPage)
BindData()
lblTotal.Text = maxPage
btnStart.Enabled = False
btnBack.Enabled = False
End If
End Sub

Protected Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
currentPage = 1
readpage(currentPage)
BindData()
btnStart.Enabled = False
btnBack.Enabled = False
btnGo.Enabled = True
btnEnd.Enabled = True
End Sub

Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
If lblIndex.Text > 2 Then
btnGo.Enabled = True
btnEnd.Enabled = True
Else
btnStart.Enabled = False
btnBack.Enabled = False
btnGo.Enabled = True
btnEnd.Enabled = True
End If
currentPage = lblIndex.Text - 1
readpage(currentPage)
BindData()
End Sub

Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
If lblIndex.Text < lblTotal.Text - 1 Then
btnStart.Enabled = True
btnBack.Enabled = True
Else
btnStart.Enabled = True
btnBack.Enabled = True
btnGo.Enabled = False
btnEnd.Enabled = False
End If
currentPage = lblIndex.Text + 1
readpage(currentPage)
BindData()
End Sub

Protected Sub btnEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnd.Click
currentPage = lblTotal.Text
readpage(currentPage)
BindData()
btnStart.Enabled = True
btnBack.Enabled = True
btnGo.Enabled = False
btnEnd.Enabled = False
End Sub
End Class

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.




Looking for the C# 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!