DataGrid中的删除和修改

博客展示了ASP中DataGrid控件的使用,包括列的设置,如隐藏主键列、超链接列等。还介绍了为DataGrid添加事件处理函数,根据CommandName实现删除和修改操作,同时提醒在page_load里添加isPostBack。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
       <Columns>
        <asp:BoundColumn Visible="False" DataField="id" DataFormatString="{0}"></asp:BoundColumn>
        <asp:HyperLinkColumn HeaderText="物资名称" DataNavigateUrlField="id" DataNavigateUrlFormatString="./ProductDetail.aspx?productID={0}"
         DataTextField="name" DataTextFormatString="{0}"></asp:HyperLinkColumn>
        <asp:BoundColumn HeaderText="型号" DataField="type" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="数量" DataField="quantity" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="单位" DataField="unit" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="截止日期" DataField="expirationDate" DataFormatString="{0}"></asp:BoundColumn>
        <asp:ButtonColumn HeaderText="删除" ButtonType="PushButton" DataTextField="id" CommandName="del" DataTextFormatString="删除{0}">
         <ItemStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Aqua"
          VerticalAlign="Middle" BackColor="Black"></ItemStyle>
        </asp:ButtonColumn>
        <asp:ButtonColumn HeaderText="修改" ButtonType="PushButton" DataTextField="id" CommandName="update"
         DataTextFormatString="修改{0}">
         <ItemStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Aqua"
          VerticalAlign="Middle" BackColor="Black"></ItemStyle>
        </asp:ButtonColumn>
       </Columns>
      </asp:DataGrid>

//第一列不显示,但是它记载着整个grid的纪录的主键,删除修改根据它来找到相应的纪录

给grid添加事件处理函数

   this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.ExcuteItemCommand);

相应的处理如下


  private void ExcuteItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string ItemID = e.Item.Cells[0].Text;
   if(e.CommandName=="del"){
    ProductManager.DeleteProduct(ItemID);
    this.Response.Redirect("./MyProductList.aspx?IsBuy="+IsBuy);
   }
   else if(e.CommandName=="update"){
    this.Response.Redirect("./UpdateProduct.aspx?productID="+ItemID+"&IsBuy="+IsBuy);
   }
  }

CommandName是从

<asp:ButtonColumn HeaderText="修改" ButtonType="PushButton" DataTextField="id" CommandName="update"
         DataTextFormatString="修改{0}">
过来的。

//需要注意的是,在page_load里面要价上一句,isPostBack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值