Introduction:
Here I will explain how to implement paging in repeater control in asp.net using c#, vb.net with example or asp.net repeater with paging in c#, vb.net or pagination in asp.net repeater control with example in c#, vb.net. To implement paging in asp.net repeater control we need to write custom logic because we don’t have any properties available for enabling pagination for repeater control.
Description:
In previous articles I explained jQuery load more data in repeater on button click in asp.net, asp.net scrollable repeater with fixed header, asp.net repeater with sorting example, Repeater Control Example in asp.net, sorting columns in repeater control in asp.net, scrollable html table with fixed header using css, Display time like facebook/twitter like 1 minute ago, hour ago, use of using statement in c# and many articles relating to asp.net, css, c#, vb.net and jQuery. Now I will explain how to implement paging in repeater control in asp.net using c#, vb.net with example.
In previous articles I explained jQuery load more data in repeater on button click in asp.net, asp.net scrollable repeater with fixed header, asp.net repeater with sorting example, Repeater Control Example in asp.net, sorting columns in repeater control in asp.net, scrollable html table with fixed header using css, Display time like facebook/twitter like 1 minute ago, hour ago, use of using statement in c# and many articles relating to asp.net, css, c#, vb.net and jQuery. Now I will explain how to implement paging in repeater control in asp.net using c#, vb.net with example.
To
implement pagination in repeater control in asp.net
using c#, vb.net
first write the code following code in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Asp.Net Repeater Control with Paging in C#, VB.NET</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Repeater ID="rptUserData"
runat="server">
<HeaderTemplate>
<table id="tbDetails"
style="width:500px; border-collapse: collapse;" border="1" cellpadding="5" cellspacing="0" >
<tr style="background-color:
#df5015; height:
30px; color:#fff" align="left">
<th>UserId</th>
<th>UserName</th>
<th>Education</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="height: 25px;">
<td >
<%#Eval("UserId").ToString()%>
</td>
<td >
<%#Eval("UserName").ToString()%>
</td>
<td>
<%#Eval("Education").ToString()%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div><br />
<asp:Repeater ID="rptPaging"
runat="server"
onitemcommand="rptPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkPage"
style="padding:8px; margin:2px; background:#B34C00; border:solid 1px #666; color:#fff; font-weight:bold"
CommandName="Page" CommandArgument="<%# Container.DataItem %>"
runat="server" Font-Bold="True"><%#
Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
|
Now open code behind file and write following code
C#
Code
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
public partial class RepeaterwithPaging
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
// Binding Repeater Control with
Paging
private void
BindRepeater()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId",
typeof(Int32));
dt.Columns.Add("UserName",
typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Rows.Add(1, "Suresh
Dasari", "B.Tech");
dt.Rows.Add(2, "Rohini
Dasari", "Msc");
dt.Rows.Add(3, "Madhav
Sai", "MS");
dt.Rows.Add(4, "Praveen",
"B.Tech");
dt.Rows.Add(6, "Sateesh",
"MD");
dt.Rows.Add(7, "Mahesh
Dasari", "B.Tech");
dt.Rows.Add(8, "Mahendra",
"CA");
PagedDataSource pageds = new
PagedDataSource();
DataView dv = new DataView(dt);
pageds.DataSource = dv;
pageds.AllowPaging = true;
pageds.PageSize = 3;
if (ViewState["PageNumber"]
!= null)
pageds.CurrentPageIndex = Convert.ToInt32(ViewState["PageNumber"]);
else
pageds.CurrentPageIndex = 0;
if (pageds.PageCount > 1)
{
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i <
pageds.PageCount; i++)
pages.Add((i + 1).ToString());
rptPaging.DataSource = pages;
rptPaging.DataBind();
}
else
{
rptPaging.Visible = false;
}
rptUserData.DataSource = pageds;
rptUserData.DataBind();
}
// Binding Data on Page Item
Change
protected void
rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"]
= Convert.ToInt32(e.CommandArgument) - 1;
BindRepeater();
}
}
|
VB.NET
Code
Imports System.Collections
Imports System.Data
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindRepeater()
End If
End Sub
' Binding Repeater with Paging
Private Sub
BindRepeater()
Dim dt As New DataTable()
dt.Columns.Add("UserId",
GetType(Int32))
dt.Columns.Add("UserName",
GetType(String))
dt.Columns.Add("Education",
GetType(String))
dt.Rows.Add(1, "Suresh
Dasari", "B.Tech")
dt.Rows.Add(2, "Rohini
Dasari", "Msc")
dt.Rows.Add(3, "Madhav
Sai", "MS")
dt.Rows.Add(4, "Praveen",
"B.Tech")
dt.Rows.Add(6, "Sateesh",
"MD")
dt.Rows.Add(7, "Mahesh
Dasari", "B.Tech")
dt.Rows.Add(8, "Mahendra",
"CA")
Dim pageds As New PagedDataSource()
Dim dv As New DataView(dt)
pageds.DataSource = dv
pageds.AllowPaging = True
pageds.PageSize = 3
If ViewState("PageNumber")
IsNot Nothing
Then
pageds.CurrentPageIndex =
Convert.ToInt32(ViewState("PageNumber"))
Else
pageds.CurrentPageIndex = 0
End If
If pageds.PageCount > 1 Then
rptPaging.Visible = True
Dim pages As New ArrayList()
For i As Integer = 0 To
pageds.PageCount - 1
pages.Add((i + 1).ToString())
Next
rptPaging.DataSource = pages
rptPaging.DataBind()
Else
rptPaging.Visible = False
End If
rptUserData.DataSource = pageds
rptUserData.DataBind()
End Sub
' Binding Data on Page Item
Change
Protected Sub
rptPaging_ItemCommand(ByVal source As Object, ByVal e As
RepeaterCommandEventArgs)
ViewState("PageNumber")
= Convert.ToInt32(e.CommandArgument) - 1
BindRepeater()
End Sub
End Class
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
8 comments :
http://www.aspdotnet-suresh.com/2016/06/aspnet-repeater-with-paging-example-in-csharp-vbnet.html
this code is not working
thx u! your tutorials are always so helpful.
Hey Suresh Dasari your tutorial is helpful for me it shows the paging just below my repeater but i got one issue that whenever i click in any of paging page number it show nothing...please help me..
I implemented your code in my project your code is so easy.
Hey Suresh your codeis helpful for me it shows the paging just below my repeater but i got one issue that whenever i click in any of paging page number it show nothing...please help me..
what if there are more than 10k of records???
Thanks a lot. It helped fix my pagination issues.
I got error here
Error:System.Web.HttpException: 'DataBinding: 'System.String' does not contain a property with the name 'fname'.'
Note: Only a member of this blog may post a comment.