Introduction:
Here I will explain how to implement paging and sorting in repeater control in asp.net using c#, vb.net with example or asp.net repeater control with paging and sorting in c#, vb.net with example or asp.net repeater paging with numbers and sort columns with example in c#, vb.net. To implement paging and sorting in asp.net repeater control we need to write custom logic because we don’t have any properties available for enabling pagination and sorting for repeater control.
Description:
In previous articles I explained asp.net repeater with paging example, Repeater Control Example in asp.net, sort columns in repeater control in asp.net, jQuery load more data in repeater on button click in asp.net, asp.net scrollable repeater with fixed header, 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 and sorting in repeater control in asp.net using c#, vb.net with example.
In previous articles I explained asp.net repeater with paging example, Repeater Control Example in asp.net, sort columns in repeater control in asp.net, jQuery load more data in repeater on button click in asp.net, asp.net scrollable repeater with fixed header, 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 and sorting in repeater control in asp.net using c#, vb.net with example.
To
implement pagination and sorting 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 & Sorting in C#,
VB.NET</title>
<style type="text/css">
.hrefclass
{
color:White;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Repeater ID="rptUserData"
runat="server"
OnItemCommand="rptUserData_ItemCommand">
<HeaderTemplate>
<table id="tbDetails"
style="width:500px; border-collapse: collapse;" border="1" cellpadding="5" cellspacing="0" >
<tr style="background-color:
#df5015; height:
30px;"
align="left">
<th>
<asp:LinkButton ID="lnkUserId"
runat="server"
CommandName="UserId"
CssClass="hrefclass">UserId</asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkUserName"
runat="server"
CommandName="UserName"
CssClass="hrefclass">UserName</asp:LinkButton></thalign>
<th>
<asp:LinkButton ID="lnkEducation"
runat="server"
CommandName="Education"
CssClass="hrefclass">Education</asp:LinkButton></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 RepeaterwithPagingSorting
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Set View State Value for
Initial Sort Order
ViewState["Column"]
= "UserId";
ViewState["Sortorder"]
= "ASC";
BindRepeater();
}
}
// Binding Repeater Control with
Paging and Sorting
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");
DataView dvData = new
DataView(dt);
PagedDataSource pageds = new
PagedDataSource();
DataView dv = new DataView(dt);
//Sorting Filter
dv.Sort = ViewState["Column"]
+ " " + ViewState["Sortorder"];
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();
}
// Bind Data for Column Sort
protected void
rptUserData_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == ViewState["Column"].ToString())
{
if (ViewState["Sortorder"].ToString()
== "ASC")
ViewState["Sortorder"]
= "DESC";
else
ViewState["Sortorder"]
= "ASC";
}
else
{
ViewState["Column"]
= e.CommandName;
ViewState["Sortorder"]
= "ASC";
}
BindRepeater();
}
// 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
'Set View State Value for Initial
Sort Order
ViewState("Column")
= "UserId"
ViewState("Sortorder")
= "ASC"
BindRepeater()
End If
End Sub
' Binding Repeater Control with
Paging and Sorting
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 dvData As New DataView(dt)
Dim pageds As New PagedDataSource()
Dim dv As New DataView(dt)
'Sorting Filter
dv.Sort = ViewState("Column")
+ " " + ViewState("Sortorder")
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
' Bind Data for Column Sort
Protected Sub
rptUserData_ItemCommand(ByVal source As Object, ByVal e As
RepeaterCommandEventArgs)
If e.CommandName = ViewState("Column").ToString()
Then
If ViewState("Sortorder").ToString()
= "ASC" Then
ViewState("Sortorder")
= "DESC"
Else
ViewState("Sortorder")
= "ASC"
End If
Else
ViewState("Column")
= e.CommandName
ViewState("Sortorder")
= "ASC"
End If
BindRepeater()
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
Check following demo for asp.net repeater control with paging and sorting example.
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.