Repeater实现排序功能(双击升序排列,再双击降序排列).原理很简单,在<TD>中加个Ondblclick,用它来触发一个LinkButton的服务器事件.LinkButton的服务器事件里重新对数据源进行排序.然后再绑定Repeater.
要实现,首先需要在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">
<td ondblclick="Getsort('Text');"><b>双击此处可排序</b></td>
后台代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
CreaterDataSource("Text ASC");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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方法的参数一定要设置为你想排序的字段名称.这样你就可以实现双击不同的列头进行当前列的排序功能.
整个实现的重点在设置DataView的sort属性,LinkButton1_Click事件中获取当前排序的字段和排序顺序的String,然后传给CreaterDataSource方法.此方法根据传来的参数将DataView的Sort重新设置,然后绑定Repeater控件.OK!
要实现,首先需要在HTML里面加个Javascript函数和两个Hidden,一个用来储存要排序的字段,还有一个用来储存升序还是降序.(asc/desc)
Javascript代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
HTML中:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
后台代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
整个实现的重点在设置DataView的sort属性,LinkButton1_Click事件中获取当前排序的字段和排序顺序的String,然后传给CreaterDataSource方法.此方法根据传来的参数将DataView的Sort重新设置,然后绑定Repeater控件.OK!