Repeater, DataList and GridView are three most famous controls in to display data in ASP.NET websites. GridView has built-in functionality of paging and you can do paging easily with GridView by setting its two properties. Sometimes some people prefer or they need to use repeater or DataList instead of GridView and they want paging functionality in these two controls. Repeater and DataList control has no built-in functionality of paging but there is a class available in ASP.NET that allows us to create paging functionality with Repeater and DataList controls. PagedDataSource is the class which provides paging related properties to do the functionality of paging. As repeater is a flexible data control in ASP.NET and many people want to use paging functionality with this control so I will show how you can do paging with repeater.
1.Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
2.Add a Web Form to Web Site
3.Write code below in your aspx page.
<table>
<asp:Label ID="lblPage" runat="server" ></asp:Label>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<th>CustomerID</th>
<th>CompanyName</th>
<th>ContactName</th>
<th>City</th>
<th>Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem, "CustomerID")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "CompanyName")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "ContactName")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "City")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "Country")%>
</td>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:LinkButton ID="lnkbtnPrev" runat="server" onclick="lnkbtnPrev_Click">Previous</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" onclick="lnkbtnNext_Click">Next</asp:LinkButton>
4.First write a property in your code file
C#
public int CurrentPageNumber
{
set
{
ViewState["PageNumber"] = value;
}
get
{
if (ViewState["PageNumber"] != null)
{
return Convert.ToInt32(ViewState["PageNumber"]);
}
else
{
return 1;
}
}
}
VB.NET
Public Property CurrentPageNumber() As Integer
Get
If ViewState("PageNumber") IsNot Nothing Then
Return Convert.ToInt32(ViewState("PageNumber"))
Else
Return 1
End If
End Get
Set(value As Integer)
ViewState("PageNumber") = value
End Set
End Property
This property is to get and set the value of current page number. We will use this property in our code.
5.Write code below in code file to get data and to do paging with repeater. Include “System.Data” and “System.Data.SqlClient” namespaces.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CustomerPagedData();
}
}
private void CustomerPagedData()
{
SqlConnection con = new SqlConnection("Data Source=YourDataSource;Initial Catalog=NORTHWND;Integrated Security=True");
con.Open();
string sql = "SELECT CustomerID, CompanyName, ContactName, City, Country FROM Customers";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
PagedDataSource pagedItems = new PagedDataSource();
pagedItems.DataSource = dt.DefaultView;
pagedItems.AllowPaging = true;
pagedItems.PageSize = 10;
pagedItems.CurrentPageIndex = CurrentPageNumber;
if (CurrentPageNumber > 1)
{
lnkbtnPrev.Visible = true;
}
else
{
lnkbtnPrev.Visible = false;
}
int rowsCount = dt.Rows.Count;
int totalPages = rowsCount / 10;
if (CurrentPageNumber < totalPages)
{
lnkbtnNext.Visible = true;
}
else
{
lnkbtnNext.Visible = false;
}
lblPage.Text = "You are at page number '" + CurrentPageNumber + "' from total of '" + totalPages + "' pages";
Repeater1.DataSource = pagedItems;
Repeater1.DataBind();
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
CustomerPagedData()
End If
End Sub
Private Sub CustomerPagedData()
Dim con As New SqlConnection("Data Source=YourDataSource;Initial Catalog=NORTHWND;Integrated Security=True")
con.Open()
Dim sql As String = "SELECT CustomerID, CompanyName, ContactName, City, Country FROM Customers"
Dim cmd As New SqlCommand(sql, con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
con.Close()
Dim pagedItems As New PagedDataSource()
pagedItems.DataSource = dt.DefaultView
pagedItems.AllowPaging = True
pagedItems.PageSize = 10
pagedItems.CurrentPageIndex = CurrentPageNumber
If CurrentPageNumber > 1 Then
lnkbtnPrev.Visible = True
Else
lnkbtnPrev.Visible = False
End If
Dim rowsCount As Integer = dt.Rows.Count
Dim totalPages As Integer = rowsCount / 10
If CurrentPageNumber < totalPages Then
lnkbtnNext.Visible = True
Else
lnkbtnNext.Visible = False
End If
lblPage.Text = "You are at page number '" + CurrentPageNumber.ToString() + "' from total of '" + totalPages.ToString() + "' pages"
Repeater1.DataSource = pagedItems
Repeater1.DataBind()
End Sub
I have connected with SQL server database and retrieved data in DataTable object. I have declared an object of PagedDataSource and set its property. I have set DataSource property to default view of DataTable, AllowPaging is set to true and page size is set to 10. CarrentPageIndex property is set to current page number. Then I have set “Previous” LinkButton visible property to true when current page is greater than first page. I have also set “Next” LinkButton visible property to true when current page is less total number of pages. It means if we reached at the last page then “Next” LinkButton will not visible. At the end Repeater control is bound to PagedDataSource object.
6.Write click events for two LinkButton controls
C#
protected void lnkbtnPrev_Click(object sender, EventArgs e)
{
CurrentPageNumber -= 1;
CustomerPagedData();
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
CurrentPageNumber += 1;
CustomerPagedData();
}
VB.NET
Protected Sub lnkbtnPrev_Click(sender As Object, e As EventArgs)
CurrentPageNumber -= 1
CustomerPagedData()
End Sub
Protected Sub lnkbtnNext_Click(sender As Object, e As EventArgs)
CurrentPageNumber += 1
CustomerPagedData()
End Sub
CurrentPageNumber property is decremented 1 in “Previous” LinkButton click. It means we will go to page before this page. CurrentPageNumber property is incremented 1 in “Next” LinkButton click. It means we will go to page after this page.
7.That’s it. See the result in browser.