Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Sorting Columns in Repeater Control in Asp.net using C#, VB.NET

Oct 19, 2014
Introduction

Here I will explain how to implement sorting in repeater control in
asp.net using c#, vb.net with example or how to sort columns in repeater control in asp.net using c#, vb.net.

Description:
  
In previous articles I explained Repeater Control Example in asp.net, Display time like facebook/twitter like 1 minute ago, hour ago, use of using statement in c#, make some cells read only in excel sheet and many articles relating to
asp.net, c#,vb.net and JQuery. Now I will explain how to implement sorting in repeater control in asp.net using c#, vb.net with example.

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</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

C# 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, "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);
        //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();
    }
}
VB.NET Code


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
    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)
        'Sorting Filter
        dvData.Sort = ViewState("Column") + " " + ViewState("Sortorder")
        rptUserData.DataSource = dvData
        rptUserData.DataBind()
    End Sub
    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
End Class
Demo


Sorting Columns in Repeater Control in Asp.net using C#, VB.NET

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Anonymous said...

Error 1 'rptUserData_ItemCommand' is not a member of 'ASP.default_aspx'. D:\Praveen@Work\WebSite1\Default.aspx 15
i am beginner in dot net i am getting this error please help...i copy pasted ur code

Bhavik Joshi said...

nice article !
but what i need to fix is repeater control Data is there only but i used dynamic data from database so DataView didn't support it .
so please help me in to fix dynamic Repeater Data Sorting by clicking Header same as here .
Thank you in advance !

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.