欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

Gridview自动排序功能的实现

时间:2020-12-06 09:24:10|栏目:.NET代码|点击:

注意两点:
1.要将gridview的AllowSorting属性置为true,同时设置OnSorting事件

2.在OnSorting事件中对排序的列设定SortExpression属性

复制代码 代码如下:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["Admin"] != "admin")
                {
                    //如果会话过期,则应该重新登录
                    this.Response.Write(" <script language=javascript>alert('你无权访问该页面,请与管理员联系!');window.location.href='../UserLogin.aspx';</script> ");
                }

复制代码 代码如下:

                ViewState["sortExpression"] = "Isdistribution";
                ViewState["sort"] = " ASC";

            }
            //绑定信息
            BindNodeInfo();
        }


        public void BindNodeInfo()
        {
            NodeLogic log = new NodeLogic();
            DataSet myset = log.GetNodeInfo();     //获取数据源
            DataView myview = myset.Tables[0].DefaultView;
            myview.Sort = ViewState["sortExpression"].ToString() +" "+ ViewState["sort"].ToString();
            this.NodeGridView.DataSource = myview;
            NodeGridView.DataKeyNames = new string[] { "node_id" };               //设置主键字段
            NodeGridView.DataBind();                                                  //绑定GridView控件 
        }

        protected void NodeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.NodeGridView.PageIndex = e.NewPageIndex;
            BindNodeInfo();
        }

        protected void NodeGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // 自动给第一列编号
            if (e.Row.RowIndex > -1)
            {
                e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
            }
        }

        protected void NodeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            NodeLogic log = new NodeLogic();
            int id = int.Parse(this.NodeGridView.DataKeys[e.RowIndex].Values[0].ToString());
            if (log.DeleteNodeInfo(id))
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功!');", true);
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败!');", true);
            //重新更新数据显示
            BindNodeInfo();
        }

        protected void NodemGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void AddNode_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNode.aspx");
        }

        protected void NodeGridView_Sorting(object sender, GridViewSortEventArgs e)
        {

            if (ViewState["sortExpression"] != null)
            {
                if (ViewState["sort"].ToString() == "Asc")
                {
                    ViewState["sort"] = "Desc";
                }
                else
                {
                    ViewState["sort"] = "Asc";

                }
            }
            BindNodeInfo();
        }

上一篇:.net平台推送ios消息的实现方法

栏    目:.NET代码

下一篇:asp.net根据日期算出天数的小例子

本文标题:Gridview自动排序功能的实现

本文地址:http://www.codeinn.net/misctech/29922.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有