Sample Code: Bidirectional Sorting in ASP.NET Repeater control

.aspx

<asp:Repeater ID="Repeater1" Runat="server" EnableViewState="False">
 <ItemTemplate>
 <table width="100%" cellspacing="0" cellpadding="0">
 <tr>
  <td  width=25%><%# DataBinder.Eval(Container.DataItem, "Employeeid") %></td>
  <td width=25% ><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
  <td width=25% ><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
  <td width=25% ><%# DataBinder.Eval(Container.DataItem, "Title") %></td>
 </tr>
 </table>
 </ItemTemplate>
 <HeaderTemplate>
 <table width="100%" cellspacing="0" cellpadding="0">
 <tr>
  <td width=25%>
  <asp:LinkButton ID="lnkEmployeeid" Runat="server" OnClick="SortEmployeeIdClick">Employeeid</asp:LinkButton>
  </td>
  <td width=25%>
  <asp:LinkButton ID="lnkFirstName" Runat="server" OnClick="SortFirstNameClick">FirstName</asp:LinkButton>
  </td>
  <td width=25%>
  <asp:LinkButton ID="lnkLastName" Runat="server" OnClick="SortLastNameClick">LastName</asp:LinkButton>
  </td>
  <td width=25% >
  <asp:LinkButton ID="lnkTitle" Runat="server" OnClick="SortTitleClick">Title</asp:LinkButton>
  </td>
 </tr>
 </table>
 </HeaderTemplate>
</asp:Repeater>

VB.NET

Dim SortField As String
Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
Dim strConn As String
Dim SQLStmt As String = "Select * from Employees  "

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ’ Put user code to initialize the page here
         BindData()
End Sub ’Page_Load

Sub BindData()
         strConn = "Server=localhost;uid=sa;password=;database=northwind;"
         myconnection = New SqlConnection(strConn)
         myda = New SqlDataAdapter(SQLStmt, myconnection)
         ds = New DataSet
         myda.Fill(ds, "AllTables")
         Repeater1.DataSource = ds
         Repeater1.DataBind()
End Sub ’BindData

Sub SortData(ByVal SortExpression As String)
         If ViewState("SortOrder") Is Nothing Then
              ViewState("SortOrder") = " ASC"
         Else
              If ViewState("SortOrder").ToString() = " ASC" Then
                  ViewState("SortOrder") = " DESC"
              Else
                  ViewState("SortOrder") = " ASC"
              End If
         End If
         SQLStmt = SQLStmt + " ORDER BY " + SortExpression.ToString() + " " + ViewState("SortOrder")
 BindData()
End Sub ’SortData

Protected Sub SortEmployeeIdClick(ByVal sender As Object, ByVal e As EventArgs)
         SortField = "EmployeeId"
         SortData(SortField)
End Sub ’SortEmployeeIdClick

Protected Sub SortFirstNameClick(ByVal sender As Object, ByVal e As EventArgs)
         SortField = "FirstName"
         SortData(SortField)
End Sub ’SortFirstNameClick

Protected Sub SortLastNameClick(ByVal sender As Object, ByVal e As EventArgs)
         SortField = "LastName"
         SortData(SortField)
End Sub ’SortLastNameClick

Protected Sub SortTitleClick(ByVal sender As Object, ByVal e As EventArgs)
         SortField = "Title"
         SortData(SortField)
End Sub ’SortTitleClick

C#
string SortField;
SqlConnection myconnection ;
SqlDataAdapter myda ;
DataSet ds ;
String strConn ;
string SQLStmt= "Select * from Employees  ";

private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
 BindData();
}

void BindData()

 strConn = "Server=localhost;uid=sa;password=;database=northwind;";
 myconnection =new SqlConnection(strConn);
 myda = new SqlDataAdapter(SQLStmt, myconnection);
 ds = new DataSet();
 myda.Fill(ds, "AllTables");
 Repeater1.DataSource = ds;
 Repeater1.DataBind();
}

void SortData(string SortExpression)
{
 if (ViewState["SortOrder"] ==null)
 {
  ViewState["SortOrder"] = " ASC";
 }
 else if (ViewState["SortOrder"].ToString () == " ASC" )
 {
  ViewState["SortOrder"] = " DESC";
 }
 else
 {
  ViewState["SortOrder"] = " ASC";
 }
 SQLStmt = SQLStmt + " ORDER BY " + SortExpression.ToString () + " " + ViewState["SortOrder"];
 BindData();
}

protected void SortEmployeeIdClick(object  sender  ,EventArgs   e   )
{
 SortField = "EmployeeId";
 SortData  (SortField);
}

protected void SortFirstNameClick(object  sender  ,EventArgs   e   )
{
 SortField = "FirstName";
 SortData  (SortField);
}

protected void SortLastNameClick(object  sender  ,EventArgs   e   )
{
 SortField = "LastName";
 SortData  (SortField);
}

protected void SortTitleClick(object  sender  ,EventArgs   e   )
{
 SortField = "Title";
 SortData  (SortField);
}

Published Saturday, October 9, 2004 12:24 AM by SushilaSB
Filed under:

Comments

Friday, February 22, 2008 11:57 AM by Krishna

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Good. How do I implement both sorting and paging in a repeater control? Thanks

Thursday, August 28, 2008 11:35 AM by Neeraj

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

it's realy a nice code..

Tuesday, November 3, 2009 1:39 PM by KodeZombie

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Thanks.  With a few modifications this got me where I wanted to go.  Instead of using SQL for our repeater, we have a web service returning an IEnumerable<myDataItem> which we were filtering with LINQ.  I just made a seperate GetQuery() method with a switch (case statement for you VB fans) to return my sorted and filtered IEnumerable<>, and added ViewState for the order by item to preserve the sort when the filter was changed.

Friday, July 23, 2010 11:45 PM by Muhammad Danish

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Its really  help full to me

Thanks

Saturday, September 11, 2010 7:02 AM by nideeshm

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Instead of foring the same query we can sort the dataTable present in dataset.

This will avoid round trip to

Wednesday, March 23, 2011 5:06 AM by parantap

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

very very nice code!!!!!!!!!!!!!!!

Thursday, April 21, 2011 8:01 AM by HEMIK

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

It's really helpful to me.

Monday, May 16, 2011 10:34 AM by Alex

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Would be better if the data were in server and not have to do a query to the database each time.

Tuesday, August 16, 2011 2:20 AM by Fulwria Interiors

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Thanx for this tutorial on repeater...

Thursday, August 18, 2011 8:10 PM by Croix

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

what if the commandtype is a stored procedure? putting an order by items in a stored procedure is a mess.

Monday, August 29, 2011 3:12 AM by Faisal.N

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

I worked fine ... need some css apply on it

Monday, August 29, 2011 12:52 PM by Arun sharma

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

ITs good but performance wise not all good,,,because one query is retierving same data again and again just differ with order....alternate of this is we can take datatable in dataview then apply sorting on that...

THanks,

Arun

Tuesday, May 29, 2012 12:47 AM by Raj

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Hoe to Fire button click event in headerTemplate of repeater in vb.net

Wednesday, August 8, 2012 1:47 AM by Nisha

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

how do we sort if we search for firstname and lastname in a textbox and Sort should be by Last Name and then by First Name..

Help plzz..

Wednesday, August 8, 2012 1:57 AM by Nisha

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

ie if the lastname is being searched in textbox, then sort should be by Last Name and then by First Name..

Wednesday, October 31, 2012 6:08 AM by Income Protection cover

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

I really like and appreciate your article.Much thanks again. Fantastic.

Saturday, November 3, 2012 1:24 PM by crork

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

1FDbhT I loved your blog article.Thanks Again. Keep writing.

Tuesday, December 25, 2012 9:51 PM by descendit thorax

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Posted by: a swedish kind of death | Nov 17, 2012 7:06:23 AM | 41 motivations or justification for wars is a guarantee that the fighting will

Friday, February 1, 2013 11:36 PM by pills for lose weight

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

JRoF5a I think this is a real great blog.Really looking forward to read more. Want more.

Monday, April 8, 2013 10:54 PM by understanding food

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

I am convinced We have look at this identical kind of statement elsewhere, it needs to be gaining popularity with all the world

Sunday, April 14, 2013 10:34 AM by Moses

# re: Sample Code: Bidirectional Sorting in ASP.NET Repeater control

Wow! At last I got a website from where I know how to really

take valuable facts concerning my study and knowledge.