当前位置:编程学习 > asp >>

DataGrid编辑,删除主要代码备用

  #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
            this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
        }
        #endregion

    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if(DataGrid1.Items.Count == 1)
            {
                if(DataGrid1.CurrentPageIndex !=0)
                {
                    DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1;
                }
            }
            us.Userid=int.Parse(e.Item.Cells[0].Text);
            us.users_del();
            bind();
        }

        private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            string str=e.SortExpression;
            if (ViewState["sort"].ToString() == str + " desc")
            {
                ViewState["sort"] = str+" asc";
            }
            else
            {
                ViewState["sort"] = str+" desc";
            }
            bind();
        }

        protected void Button1_Click(object sender, System.EventArgs e)
        {
            string str = TextBox1.Text;
            ViewState["chazao"] ="useradmin like '%"+str+"%'";
            bind();
       
        }

        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            bind();
        }
        protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            DataSet ds=  us.users_sel();
            DataView  dv= ds.Tables[0].DefaultView;
            DataGrid1.EditItemIndex = e.Item.ItemIndex;
            DataGrid1.DataSource=dv;
            DataGrid1.DataBind();
        }
        protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            DataSet ds = us.users_sel();
            DataView dv = ds.Tables[0].DefaultView;
            DataGrid1.EditItemIndex = -1;
            DataGrid1.DataSource = dv;
            DataGrid1.DataBind();
        }
        protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            string useradmin = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
        

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,