Tutorial RSS
 
DB Tutorials Server Intellect Cloud Hosting
Navigator: Home - Advanced - Implementing a Pager with the Repeater Control in VB

Implementing a Pager with the Repeater Control in VB

This tutorial will show how we can add a pager to a repeater control, and use a querystring value to move between the pages. This method is much more SEO-friendly than using postback, like the built-in GridView pager. VB version.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!


Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

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 references:

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient

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>

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>

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!



The code-behind should look something like this:

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DisplayData()
End Sub

Public Sub DisplayData()
Dim myConnection As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
Dim cmd As New SqlCommand("SELECT * FROM [Table1]", myConnection)
cmd.Connection.Open()

Dim myDA As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
myDA.Fill(ds, "Table1")
Dim pageds As New PagedDataSource()
pageds.DataSource = ds.Tables("Table1").DefaultView
pageds.AllowPaging = True
pageds.PageSize = 3

Dim curpage As Integer = 0

If Request.QueryString("page") IsNot Nothing Then
curpage = Convert.ToInt32(Request.QueryString("page"))
Else
curpage = 1
End If

pageds.CurrentPageIndex = curpage - 1

If curpage = 1 AndAlso pageds.DataSourceCount > pageds.PageSize Then
lblCurrpage.Text = "Pages: 1"
ElseIf pageds.DataSourceCount = 0 Then
lblCurrpage.Text = "No data to display."
ElseIf curpage > 1 AndAlso pageds.DataSourceCount > pageds.PageSize Then
lblCurrpage.Text = "Pages: <a href='Default.aspx?page=1'>1</a>"
End If
For i As Integer = 2 To pageds.PageCount
If i = curpage Then
lblCurrpage.Text = lblCurrpage.Text & ", " & i.ToString()
Else
lblCurrpage.Text = lblCurrpage.Text & ", <a href='Default.aspx?page=" & i.ToString() & "'>" & i.ToString() & "</a>"
End If
Next i

Repeater1.DataSource = pageds
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub
End Class

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.




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