前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GridView行编辑、更新、取消、删除事件使用方法

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

作者头像
全栈程序员站长
发布2022-07-12 17:43:19
1.3K0
发布2022-07-12 17:43:19
举报

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

注意:当启用编辑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(); }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118540.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档