ListView排序(code behind)

Posted petewell

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView排序(code behind)相关的知识,希望对你有一定的参考价值。

摘要:ListView排序(code behind)


.ASPX 内容


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

.cs 后端内容


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

原文:大专栏  ListView排序(code behind)


以上是关于ListView排序(code behind)的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Asp.Net Code Behind 调用 JavaScript 函数?

How do I duplicate a resource reference in code behind in WPF?

从Script到Code BlocksCode Behind到MVCMVPMVVM(转载)

将字符串或属性传递给 WPF Code Behind

MySQL故障处理 Seconds_Behind_Master= NULL Error_code: 1197

Bootstrap Style & Surrounding Div tag from ASP.NET Code Behind [重复]