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!