如何使用c#将参数传递给click事件方法

Hat*_*tem 5 c# asp.net events

我在C#上下文中有这个按钮:

<button id='Update' OnServerClick='UpdateCart'>Update</button>
Run Code Online (Sandbox Code Playgroud)

现在我想将参数(num)传递给(UpdateCart)方法:

protected void UpdateCart(object sender, EventArgs e)
{

    Response.Cookies["Cart"]["Qty(" + num + ")"] = Request.Form["Qty" + num];
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

ada*_*ost 6

使用ASP.NET Button控件而不是<button/>标记,允许您通过CommandArgument属性设置值并使用Command事件(不要使用Click)来检索CommandArgument属性值.

标记:

 <asp:Button 
              ID="Button1" 
              runat="server" 
              CommandArgument="10" 
              oncommand="Button1_Command" 
              Text="Button" />
Run Code Online (Sandbox Code Playgroud)

码:

protected void Button1_Command(object sender, CommandEventArgs e)
  {
      string value = e.CommandArgument.ToString();
  }
Run Code Online (Sandbox Code Playgroud)


小智 4

您将使用“commandArgument”属性。这是MSDN上的一个例子

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx