ListView排序(code behind)

Abstract: ListView Sort (code behind)


.ASPX content


 
  
  
        
  
  
        
  
   
            
   
    
                
    
     
      
    
      
                        <%----%>
                        
      
      
<%# Eval("HOST_NAME") %> <%# Eval("HOSTID") %> <%# Eval("STATUS") %>

.cs back-end content


public partial class ListViewFrom : System.Web.UI.Page
    {
        DBHelper db = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBHelper();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //将排序的ViewState设定为null,新的查询才会为初始的排序
            ViewState["SortExpression"] = null;
            this.Bind(ViewState["SortExpression"]);
        }

        private void Bind(object oSortExpression)
        {
            //当查询语法的TextBox为空时return
            if (this.TextBox1.Text.Trim().Equals("")) return;
            string sSQL = this.TextBox1.Text.Trim();
            if (oSortExpression != null) //当传入的排序参数不为null将串成SQL语法
                sSQL = this.TextBox1.Text.Trim() + oSortExpression.ToString();

            //进行查询
            DataSet ds = db.QueryData(sSQL);
            //设定ListView的数据来源
            this.ListView1.DataSource = ds;
            //进行数据Bind动作
            this.ListView1.DataBind();
        }

        protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
        {
            //取得ListView的分页组件
            DataPager dp = this.ListView1.FindControl("_moviesGridDataPager") as DataPager;
            //取得每页所要显示的笔数
            int pagesize = dp.PageSize;
            //取得所要浏览的分页号码
            int startRowIndex = dp.StartRowIndex;
            //取得数据笔数
            int totalRowCount = dp.TotalRowCount;
            //设定分页组件所要显示的分页号和每页所显示的笔数
            dp.SetPageProperties(startRowIndex, pagesize, true);
            //数据设定
            this.Bind(ViewState["SortExpression"]);
        }

        protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {
            string sSortDirection = "ASC";
            //当ViewState的Key为SortExpression非null时
            if (ViewState["SortExpression"] != null)
            {
                //判断SortExpression的Value字符串是否包含ASC
                if (ViewState["SortExpression"].ToString().Contains(sSortDirection))
                {
                    //代表该字段要设为DESC
                    sSortDirection = " DESC ";
                }
            }
            else
            {
                sSortDirection = " DESC ";
            }
            //SortExpression的Value排序 e.SortExpression为要排序的字段,sSortDirection为本次的排序方式
            ViewState["SortExpression"] = " ORDER BY " + e.SortExpression + " " + sSortDirection;
            this.Bind(ViewState["SortExpression"]);
        }
    }

Original: Large column  ListView sort (code behind)


Guess you like

Origin www.cnblogs.com/petewell/p/11518140.html