GridView基本的操作 编辑 更新 取消 删除

115 篇文章 2 订阅

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

<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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值