ASP.NET 页面多个button共用一个事件进行增删改查

//页面代码
  1.  <asp:Button ID="Button2" runat="server" Text="修改" OnCommand="Button3_Command" CommandArgument='<%# Bind("Id") %>'  CommandName="update" OnClientClick="return confirm('确定修改么?')"/>
  2.  <asp:Button ID="Button3" runat="server" Text="删除" OnCommand="Button3_Command" CommandName="del" CommandArgument='<%# Bind("Id") %>' OnClientClick="return confirm('确定删除么?')"/>

//CommandArgument获取要修改的数据ID     CommandName 指进行删除或者修改操作  OnCommand 指公用事件名称

//后台代码

 

  1. protected void Button3_Command(object sender, CommandEventArgs e)
  2.     {
  3.         if (e.CommandName=="update")
  4.         {
  5.             Response.Redirect("gchanpin.aspx?id=" + e.CommandArgument.ToString());
  6.         }
  7.         if (e.CommandName=="del")
  8.         {
  9.             if (!BLL.Chanpin.DeleteMethod(e.CommandArgument.ToString()))
  10.             {
  11.                 Response.Write("<script>alert('删除失败');history.back();</script>");
  12.                 Response.End();
  13.             }
  14.             else
  15.             {
  16.                 BindList();
  17.             }
  18.         }
  19.     }

你可能感兴趣的:(server,command,asp.net,asp,button)