Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  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 - How to page Repeater using ASP.NET 2.0 and VB.NET
How to page Repeater using ASP.NET 2.0 and VB.NET


ASP.NET Controls Tutorial

This tutorial will show you 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.

Imports System.Data.SqlClient

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 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.

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

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

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>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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

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

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks