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 VB.NET
Using a Paged Data Source with ASP.NET and VB.NET


ASP.NET Controls Tutorial

This tutorial will show how we can use a Paged Data Source to create pages within a Repeater Control. VB 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:

Imports System.Data.SqlClient

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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>

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!

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>

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

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

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
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 Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
DisplayData()
End Sub

Public Property PageNumber() As Integer
Get
If ViewState("PageNumber") IsNot Nothing Then
Return Convert.ToInt32(ViewState("PageNumber"))
Else
Return 0
End If
End Get
Set(ByVal value As Integer)
ViewState("PageNumber") = value
End Set
End Property

Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
AddHandler repeaterPager.ItemCommand, AddressOf repeaterPager_ItemCommand
End Sub

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

Dim myReader As SqlDataReader = cmd.ExecuteReader()
Dim myTable As New DataTable()
myTable.Load(myReader)

Dim pgitems As New PagedDataSource()
Dim dv As 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 Then
repeaterPager.Visible = True
Dim pages As New ArrayList()
For i As Integer = 0 To pgitems.PageCount - 1
pages.Add((i + 1).ToString())
Next i
repeaterPager.DataSource = pages
repeaterPager.DataBind()
Else
repeaterPager.Visible = False
End If
Repeater1.DataSource = pgitems

Repeater1.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

Private Sub repeaterPager_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
PageNumber = Convert.ToInt32(e.CommandArgument) - 1
DisplayData()
End Sub
End Class

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks