zoukankan      html  css  js  c++  java
  • 实现Repeater控件点击表头排序功能

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

    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方法的参数一定要设置为你想排序的字段名称.这样你就可以实现双击不同的列头进行当前列的排序功能.整个实现的重点在设置 DataView的sort属性,LinkButton1_Click事件中获取当前排序的字段和排序顺序的String,然后传给 CreaterDataSource方法.此方法根据传来的参数将DataView的Sort重新设置,然后绑定Repeater控件.OK!

    另外:http://blog.csdn.net/cxzhq2002/archive/2009/05/05/4151588.aspx

    Repeater 双向排序 

  • 相关阅读:
    1451. Rearrange Words in a Sentence
    1450. Number of Students Doing Homework at a Given Time
    1452. People Whose List of Favorite Companies Is Not a Subset of Another List
    1447. Simplified Fractions
    1446. Consecutive Characters
    1448. Count Good Nodes in Binary Tree
    709. To Lower Case
    211. Add and Search Word
    918. Maximum Sum Circular Subarray
    lua 时间戳和时间互转
  • 原文地址:https://www.cnblogs.com/blsong/p/1636140.html
Copyright © 2011-2022 走看看