GridView行编辑、更新、取消、删除事件用法

 

注意:当启用编辑按钮时,点击编辑按钮后会使一整行都切换成文本框。为了是一行中的一部分是文本框,需要把以整行的所有列都转换成模板,然后删掉编辑模板中的代码。这样就能使你想编辑的列转换成文本框。

1.界面

            GridLines="None" AutoGenerateColumns="False"  DataKeyNames="ProductID"
            onrowdatabound="GridView1_RowDataBound" AllowPaging="True"
            onpageindexchanging="GridView1_PageIndexChanging"
            onrowcommand="GridView1_RowCommand"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
            onrowdeleting="GridView1_RowDeleting">
                            Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
           
           
               
 
                   
                       
                   

               

               
                   
                       
                   

                   
                       
                   

               

               
                   
                       
                   

               

               

                   
                        删除
                      
                   

               
               
               
                   
                       
                   

           
                            HorizontalAlign="Right" />
           
           
           
           
           
       

 

2.前台操控调用业务

DalBll db = new DalBll();
    static List tmpList = new List();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if(!IsPostBack)
        {
            InitGridView();
        }
    }

    private void InitGridView()
    {
        tmpList = db.GetDataList();
        this.GridView1.DataSource = tmpList;
        this.GridView1.DataBind();
    }

    //绑定数据时触发
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Products tmp = e.Row.DataItem as Products;
            LinkButton lbtn = e.Row.FindControl("del") as LinkButton;
            if (lbtn != null && tmp != null)
                lbtn.CommandArgument = tmp.ProductID.ToString();//绑定主键
        }
    }

    //删除数据
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "dell")
        {
            int productID = Convert.ToInt32(e.CommandArgument);
            db.Del(productID);
        }

    }

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        InitGridView();
    }

    //切换到编辑模式
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        InitGridView();
    }

    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        InitGridView();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取当前页索引
        int i = this.GridView1.EditIndex;
        //获取文本框的值
        string productsName = ((TextBox)(this.GridView1.Rows[i].FindControl("txtName"))).Text.ToString();
        DataKey key = this.GridView1.DataKeys[e.RowIndex];
        int id = Convert.ToInt32(key[0].ToString());
        //首先找到该对象
        Products tmp = tmpList.Where(c => c.ProductID == id).First();
        tmp.ProductName = productsName;
        db.Update(tmp);
        InitGridView();
    }


3.各操作数据的代码

public class DalBll
    {
        NorthwindEntities db = new NorthwindEntities();

        public List GetDataList()
        {
            return db.Products.ToList();
        }

        public void Del(int productID)
        {
            Products tmp = db.Products.Where(c=>c.ProductID==productID).First();
            db.DeleteObject(tmp);
            db.SaveChanges();
        }

        public void Update(Products tmp)
        {
            //为参数对象创建实体键
            EntityKey key;
            object originalProductObj;
            //因参数对象不属于上下文,因此为该参数对象创建上下文实体键
            key = db.CreateEntityKey("Products", tmp);
            db.TryGetObjectByKey(key, out originalProductObj);
            db.ApplyPropertyChanges(key.EntitySetName, tmp);
            db.SaveChanges();
           
        }

 

你可能感兴趣的:(ASP.NET)