Repeater click two-way sorting of the header

Source: Internet
Author: User
Repeater supports sorting (double-click Sort in ascending order, and then double-click Sort in descending order ). the principle is very simple. in <TD>, add an ondblclick to trigger a linkbutton server event. in the server event of linkbutton, sort the data source again. then bind repeater.
To achieve this, you must first add a JavaScript function and two den functions in HTML, one for storing the field to be sorted, and the other for storing the ascending or descending order. (ASC/DESC)
Javascript Code As follows:
<Script language = "JavaScript">
Function getsort (OBJ)
{
Document. All. sortfield. value = OBJ;
If (document. All. sortstring. value = 'asc ')
{
Document. All. sortstring. value = 'desc ';
}
Else if (document. All. sortstring. value = 'desc ')
{
Document. All. sortstring. value = 'asc ';
}
_ Dopostback ('linkbutton1 ','');
}
</SCRIPT>
In HTML:
<Input type = "hidden" id = "sortfield" runat = "server">

<Input type = "hidden" id = "sortstring" runat = "server" value = "ASC">
<Asp: linkbutton id = "linkbutton1" runat = "server" visable = "false"> linkbutton </ASP: linkbutton>

<TD ondblclick = "getsort ('text');"> <B> double-click here to sort </B> </TD>
The background code is as follows:
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Createrdatasource ("text ASC ");
}
}

Private void createrdatasource (string sort)
{
Oledbconnection dbcon = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("test. mdb "));
Dbcon. open ();
Oledbdataadapter adapter = new oledbdataadapter ("select * from table", dbcon );

Datatable dt = new datatable ();
Adapter. Fill (DT );
Dataview DV = DT. defaultview;
DV. Sort = sort;
Repeater1.datasource = DV;
Repeater1.databind ();
}

Private void linkbutton#click (Object sender, system. eventargs E)
{
String sortstring = request. Form ["sortstring"]. tostring ();
String sortfield = request. Form ["sortfield"]. tostring ();
String fullsortstring = sortfield + "" + sortstring;
If (fullsortstring! = "" & Fullsortstring! = NULL)
{
Createrdatasource (fullsortstring );
}

}

The above mainly lists the code for implementing the sorting function. in ondblclick in HTML, you can give different parameters of the getsort method to sort different fields. Note that the parameter of the getsort method must set the name of the field you want to sort. in this way, you can double-click different column headers to sort the current column.

The focus of the entire implementation is to set the sort attribute of repeater. In the linkbutton1_click event, obtain the field of the current sorting and the string of the sorting order, and then pass it to the createrdatasource method. this method resets the sort of dataview Based on the sent parameters, and then binds the Repeater control. OK!

 

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.