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 09, 2004 12:24 AM by SushilaSB
Filed under:

Comments

Sunday, April 10, 2005 7:06 AM by TrackBack

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

^_^,Pretty Good!
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 03, 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

Wednesday, January 04, 2012 4:15 AM by puntypricilla

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

buy best <a href=www.ugg-store-ok.com/london-ugg-store-ezp-5.html>london ugg store</a>  suprisely   with confident

Thursday, January 05, 2012 6:34 AM by beedojina

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

buy a <a href=www.wearol.com/special-occasion-dresses-prom-dresses-c-9_1.html>Cheap Prom Dresses</a>  with low price    and get big save

Thursday, January 05, 2012 9:07 PM by Lessyidalia

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

must check <a href=chanelhandbags2010.yolasite.com/>chanel handbags 2010</a>   and get big save   suprisely

Sunday, January 08, 2012 8:54 PM by Quomsalvin

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

buy best <a href=dvd-h264.weebly.com/>dvd h.264</a>   and check coupon code available    for promotion code

Tuesday, January 10, 2012 11:56 PM by DierEdustin

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

view <a href=chanelmirror.eklablog.com/>chanel mirror</a>  for gift   online

Tuesday, January 17, 2012 7:03 AM by Ceteivan

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

you love this?     to take huge discount <a href=www.indianafortwayne.com/.../a>   to take huge discount

Sunday, January 29, 2012 9:43 AM by GomBlodo

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

buy a <a href=chanel-hobo-bag.weebly.com/>chanel hobo bag</a>  for more

Leave a Comment

(required) 
(required) 
(optional)
(required)