GridView基本的操作 编辑 更新 取消 删除
2011-11-22 16:09
726 查看
1.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class ST_Admin_proman : ST_DataBase { protected void Page_Load(object sender, EventArgs e) { if (Session["admin"] != null) { Response.Redirect("ST_contraller.aspx?cname=noadmin"); } if (!IsPostBack) { ShowProdut(); } } protected void ShowProdut() { DataSet ds = new DataSet(); string st_sql = "SELECT * FROM ST_tProduct order by ST_ID desc"; ds = GetDataSet(st_sql, "Product"); GridView1.DataSource = ds; GridView1.DataBind(); } protected void Grid_Del(object sender, GridViewDeleteEventArgs e) { int index = e.RowIndex; GridViewRow row = GridView1.Rows[index]; string row_cell = row.Cells[0].Text.Trim(); string st_sql = "delete FROM ST_tProduct Where ST_ID='" + row_cell + "'"; try { Exesql(st_sql); ShowMsg.Text = "删除产品成功"; ShowMsg.Style["color"] = "Green"; } catch (Exception ex) { ShowMsg.Text = "删除产品失败,原因:" + ex.Message; ShowMsg.Style["color"] = "red"; } finally { ShowProdut(); } } protected void Grid_Change(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; ShowProdut(); } protected void Grid_Edit(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; ShowProdut(); } protected void Grid_Cancel(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; ShowProdut(); } protected void Grid_Updating(object sender, GridViewUpdateEventArgs e) { int index = e.RowIndex; GridViewRow row = GridView1.Rows[index]; string st_sql; st_sql = "update ST_tProduct set st_productname= '" + ((TextBox)(row.Cells[1].Controls[0])).Text + "',st_productprice= '" + ((TextBox)(row.Cells[2].Controls[0])).Text + "',st_productpic='" + ((TextBox)(row.Cells[3].Controls[0])).Text + "' where ST_ID='" + row.Cells[0].Text.Trim() + "'"; try { Exesql(st_sql); GridView1.EditIndex = -1; ShowMsg.Text = "更新产品成功" + st_sql; ShowMsg.Style["color"] = "Green"; } catch (Exception ex) { ShowMsg.Text = "更新产品失败,原因:" + ex.Message; ShowMsg.Style["color"] = "red"; } finally { ShowProdut(); } } }
2.html
![](http://hi.csdn.net/attachment/201111/22/0_1321950577Nuf6.gif)
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div style="width:600px"> <table> <tr> <td> <asp:GridView ID="GridView1" runat="server" AllowPaging="true" AutoGenerateColumns="false" OnRowDeleting="Grid_Del" OnPageIndexChanging="Grid_Change" OnRowEditing="Grid_Edit" OnRowUpdating="Grid_Updating" OnRowCancelingEdit="Grid_Cancel" PagerSettings-Mode="NextPreviousFirstLast" PagerSettings-NextPageText="下一页" PagerSettings-PreviousPageText="上一页" Width="600"> <Columns> <asp:BoundField DataField="st_id" HeaderText="产品编号" ReadOnly="true" /> <asp:BoundField DataField="st_productname" HeaderText="产品名称" /> <asp:BoundField DataField="st_productprice" HeaderText="产品价格" /> <asp:BoundField DataField="st_productpic" HeaderText="产品图片" /> <asp:BoundField DataField="st_productclass" HeaderText="产品类型" ReadOnly="true" /> <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowCancelButton="true" HeaderText="修改" /> <asp:ButtonField Text="删除" ButtonType="Button" HeaderText="删除" CommandName="delete" /> </Columns> </asp:GridView> </td> </tr> <tr> <td><asp:Label ID="ShowMsg" runat="server"></asp:Label></td> </tr> </table> </div> </form> </body> </html>
相关文章推荐
- GridView的操作:基本操作(编辑、更新、取消)
- GridView基本的操作 编辑 更新 取消
- asp.net c# 对GridView编辑,删除,更新,取消的操作
- GridView基本的操作 编辑 更新 取消
- GridView基本的操作 编辑 更新 取消(一)
- GridView基本的编辑,更新,删除操作
- GridView基本的编辑,更新,删除操作
- GridView基本的操作 编辑 更新 取消(二)
- GridView基本的操作 编辑 更新 取消(三)
- GridView基本的编辑,更新,删除操作
- GridView行编辑、更新、取消、删除事件用法
- GridView绑定,编辑,更新,取消,删除,排序
- WebForm GridView设置分页,编辑,更新,取消,删除,按钮,以及排序。
- GridView自定义模板的编辑,更新,取消,删除
- GridView中用CommandField实现简单的编辑(更新,取消),删除
- Gridview中的选择、删除、编辑、更新、取消留着备用。
- GridView操作大全 无代码分页排序 正反双向排序 和下拉菜单DropDownList结合,选中,编辑,取消,删除
- 在GridView中应该用分页、编辑、删除、更新操作
- GridView自定义模板编辑 更新 取消 删除
- GridView操作大全 无代码分页排序 正反双向排序 和下拉菜单DropDownList结合,选中,编辑,取消,删除