要实现,首先需要在HTML里面加个javascript函数和两个Hidden,一个用来储存要排序的字段,还有一个用来储存升序还是降序.(asc/desc)
Javascript代码如下:
<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>
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>
后台代码如下:
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 LinkButton1_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);
}
}
上面主要列举了排序功能实现的代码.在HTML中的ondblclick中,您可以给Getsort方法不同的参数以获得对不同的字段进行排序,注意, Getsort方法的参数一定要设置为你想排序的字段名称.这样你就可以实现双击不同的列头进行当前列的排序功能.
整个实现的重点在设置 Repeater的sort属性,LinkButton1_Click事件中获取当前排序的字段和排序顺序的String,然后传给 CreaterDataSource方法.此方法根据传来的参数将DataView的Sort重新设置,然后绑定Repeater控件.OK!