ASP.NET Repeater Paging

The ASP.NET Repeater control serves as a fundamental container control that enables developers to create customized lists using data from various sources available on the web page. It provides the capability to render HTML and display the contents of a list or data source to which it is bound.

When working with the Repeater control, developers may sometimes require the implementation of a custom pager, which allows users to navigate through the records in the list. To achieve this functionality, several components are typically utilized. The following ASP.NET program shows how to create a custom pager to the Reapeater control.

repeater-paging

LinkButton controls

Firstly, two LinkButton controls are commonly used to enable navigation between the next and previous records in the list. These LinkButtons can be styled and positioned as desired and are typically associated with appropriate event handlers to handle the navigation logic.

Additionally, a hidden field is utilized to store and maintain the current index or position of the recordset. This hidden field serves as a means to carry the necessary information between postbacks and allows the pager to keep track of the current position in the list.

By combining these components, developers can build a custom pager for the Repeater control that enhances the user experience by providing navigation capabilities through the list of records. Users can click the LinkButtons to move to the next or previous set of records, while the hidden field helps maintain the correct state and position within the recordset. Here we are using this index value for retrieving the next group of data from recordset.

adapter.Fill(dataset, startRecordNo, NoOfRecords, "authors")

The following ASP.NET program retrieves the data from Authors table and display 5 records per page in the Repeater control.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater id="repeater1" runat="server" > <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "au_id")%>    <%# DataBinder.Eval(Container.DataItem, "au_lname")%> <br /> </ItemTemplate> <SeparatorTemplate> <hr> </SeparatorTemplate> </asp:Repeater> </div> <input id="txtHidden" style="width: 28px" type="hidden" value="0" runat="server" /> <hr> <asp:LinkButton ID="lnkBtnPrev" runat="server" Font-Underline="False" OnClick="lnkBtnPrev_Click" Font-Bold="True"><< Prev </asp:LinkButton>    <asp:LinkButton ID="lnkBtnNext" runat="server" Font-Underline="False" OnClick="lnkBtnNext_Click" Font-Bold="True">Next >></asp:LinkButton> </form> </body> </html>
Full Source | C#
using System; using System.Data ; using System.Data.SqlClient ; using System.Web.UI.WebControls; using System.Configuration; public partial class _Default : System.Web.UI.Page { int totalCount = 0; protected void Page_Load(object sender, EventArgs e) { string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString(); SqlConnection connection = new SqlConnection(connectionString); DataSet ds = new DataSet(); String sql = "select [au_id],[au_lname] from authors order by au_id"; SqlDataAdapter adapter = new SqlDataAdapter(sql, connection); adapter.Fill(ds); totalCount = ds.Tables[0].Rows.Count; bindData(); } public void bindData() { string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString(); SqlConnection connection = new SqlConnection(connectionString); DataSet ds = new DataSet(); String sql = "select [au_id],[au_lname] from authors order by au_id"; int val = Convert.ToInt16(txtHidden.Value); if (val <= 0) val = 0; connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sql, connection); adapter.Fill(ds, val, 5, "authors"); connection.Close(); repeater1.DataSource = ds; repeater1.DataBind(); if (val <= 0) { lnkBtnPrev.Visible = false; lnkBtnNext.Visible = true; } if (val >= 5) { lnkBtnPrev.Visible = true; lnkBtnNext.Visible = true; } if ((val+5)>=totalCount ) { lnkBtnNext.Visible = false; } } protected void lnkBtnPrev_Click(object sender, EventArgs e) { txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) - 5); bindData(); } protected void lnkBtnNext_Click(object sender, EventArgs e) { txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) + 5); bindData(); } }
Full Source | VB.NET
Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI.WebControls Partial Class _Default Inherits System.Web.UI.Page Dim totalCount As Integer = 0 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim connectionString As String = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString() Dim connection As New SqlConnection(connectionString) Dim ds As New DataSet() Dim sql As [String] = "select [au_id],[au_lname] from authors order by au_id" Dim adapter As New SqlDataAdapter(sql, connection) adapter.Fill(ds) totalCount = ds.Tables(0).Rows.Count If Not IsPostBack Then bindData() End If End Sub Public Sub bindData() Dim connectionString As String = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString() Dim connection As New SqlConnection(connectionString) Dim ds As New DataSet() Dim sql As [String] = "select [au_id],[au_lname] from authors order by au_id" Dim val As Integer = Convert.ToInt16(txtHidden.Value) If val <= 0 Then val = 0 End If connection.Open() Dim adapter As New SqlDataAdapter(sql, connection) adapter.Fill(ds, val, 5, "authors") connection.Close() repeater1.DataSource = ds repeater1.DataBind() If val <= 0 Then lnkBtnPrev.Visible = False lnkBtnNext.Visible = True End If If val >= 5 Then lnkBtnPrev.Visible = True lnkBtnNext.Visible = True End If If (val + 5) >= totalCount Then lnkBtnNext.Visible = False End If End Sub Protected Sub lnkBtnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnPrev.Click txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) - 5) bindData() End Sub Protected Sub lnkBtnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnNext.Click txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) + 5) bindData() End Sub End Class

Conclusion

The ASP.NET Repeater control offers the flexibility to create custom lists using data from various sources. When implementing a custom pager for the Repeater control, developers typically utilize LinkButton controls for navigation and a hidden field to store the current index or position of the recordset. By combining these components, developers can enhance the functionality and user experience of the Repeater control, providing seamless navigation through the list of records.