Sorting Columns in Repeater Control
Here I will explain how to implement sorting in repeater control in asp.net using c#.
To implement sorting in repeater control first create new web application and write the following code in aspx page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Repeater Sorting Example in ASP.NET,C#</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 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>
</form>
</body>
</html>
<head id="Head1" runat="server">
<title>Repeater Sorting Example in ASP.NET,C#</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 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>
</form>
</body>
</html>
Now open code behind file and write following code
using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class _Default : 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();
}
}
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, "Sumit Kemar Sen", "B.Tech");
dt.Rows.Add(2, "Soumi Santra", "MA");
dt.Rows.Add(3, "Sujoy Santra", "MCA");
dt.Rows.Add(4, "Pradipta Chowdhury", "MCA");
dt.Rows.Add(6, "Sourav Mukherjee", "B.Tech");
dt.Rows.Add(7, "Sandipan Maji", "B.Tech");
dt.Rows.Add(8, "Mithun Patra", "MCA");
DataView dvData = new DataView(dt);
//Sorting Filter
dvData.Sort = ViewState["Column"] + " " + ViewState["Sortorder"];
rptUserData.DataSource = dvData;
rptUserData.DataBind();
}
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();
}
}
using System.Data;
using System.Web.UI.WebControls;
public partial class _Default : 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();
}
}
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, "Sumit Kemar Sen", "B.Tech");
dt.Rows.Add(2, "Soumi Santra", "MA");
dt.Rows.Add(3, "Sujoy Santra", "MCA");
dt.Rows.Add(4, "Pradipta Chowdhury", "MCA");
dt.Rows.Add(6, "Sourav Mukherjee", "B.Tech");
dt.Rows.Add(7, "Sandipan Maji", "B.Tech");
dt.Rows.Add(8, "Mithun Patra", "MCA");
DataView dvData = new DataView(dt);
//Sorting Filter
dvData.Sort = ViewState["Column"] + " " + ViewState["Sortorder"];
rptUserData.DataSource = dvData;
rptUserData.DataBind();
}
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();
}
}