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


     

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

    1.界面

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
                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">
                <PagerSettings FirstPageText="首页" LastPageText="尾页"
                    Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
                <RowStyle BackColor="#E3EAEB" />
                <Columns>
                    <asp:TemplateField HeaderText="ProductID">
     
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ProductName">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="UnitPrice">
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="操">

                        <ItemTemplate>
                            <asp:LinkButton ID="del" runat="server" OnClientClick="return confirm('您确定要删除吗?')" CommandName="dell" >删除</asp:LinkButton>
                          
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" HeaderText="作">
                   
                        <HeaderStyle  HorizontalAlign="Center" />
                            <ItemStyle  HorizontalAlign="Center"  Width="85px" ForeColor="Blue" />
                        </asp:CommandField>
                </Columns>
                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White"
                    HorizontalAlign="Right" />
                <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#7C6F57" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

    2.前台操控调用业务

    DalBll db = new DalBll();
        static List<Products> tmpList = new List<Products>();
        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<Products> 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();
               
            }

  • 相关阅读:
    VC++ 之 文件操作
    Delphi7 API(5) 消息篇:WM_LBUTTONDOWN、WM_LBUTTONUP、WM_MOUSEMOVE
    VC++ 之 输入/输出类库(二)
    VB 访问控制面板
    Delphi7 API(4) 消息_重绘
    Lisp简明教程
    一次快速排序错误引发的思考(2)
    一次快速排序错误引发的思考(1)
    Common Lisp编译程序的小技巧
    暴风影音5免去广告的小技巧
  • 原文地址:https://www.cnblogs.com/yxwkf/p/3942372.html
Copyright © 2020-2023  润新知