程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Gridview主動排序功效的完成

Gridview主動排序功效的完成

編輯:C#入門知識

Gridview主動排序功效的完成。本站提示廣大學習愛好者:(Gridview主動排序功效的完成)文章只能為提供參考,不一定能成為您想要的結果。以下是Gridview主動排序功效的完成正文


留意兩點:
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();
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved