欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Gridview自动排序功能的实现

程序员文章站 2023-12-22 19:34:28
注意两点:1.要将gridview的allowsorting属性置为true,同时设置onsorting事件 2.在onsorting事件中对排序的列设定sortexpr...

注意两点:
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();
        }

上一篇:

下一篇: