码迷,mamicode.com
首页 > 编程语言 > 详细

ListView排序(code behind)

时间:2019-09-14 10:59:36      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:ring   man   语法   lin   prope   run   查询   nta   field   

摘要: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)

标签:ring   man   语法   lin   prope   run   查询   nta   field   

原文地址:https://www.cnblogs.com/petewell/p/11518140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!