Sample Code: bidirectional sorting in ASP. NET Repeater control

Source: Internet
Author: User

. 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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.