Hatem
Hatem

Reputation: 493

How to pass parameters to click event method using c#

i have this button on my C# context:

<button id='Update' OnServerClick='UpdateCart'>Update</button>

now I wanna to pass parameter (num) to (UpdateCart) Method :

protected void UpdateCart(object sender, EventArgs e)
{

    Response.Cookies["Cart"]["Qty(" + num + ")"] = Request.Form["Qty" + num];
}

How I can do that ?

Upvotes: 5

Views: 19832

Answers (3)

Anicho
Anicho

Reputation: 2667

I noticed that command argument does not accept dynamically set values from client side, command argument must be set server side. So to combat this I used asp:hidden field updated its value and then from code behind got the value by just doing hiddenfield.value.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94635

Use ASP.NET Button control instead of <button/> markup that allow you to set value via CommandArgument property and use Command event (do not use Click) to retrieve value of CommandArgument property.

Markup:

 <asp:Button 
              ID="Button1" 
              runat="server" 
              CommandArgument="10" 
              oncommand="Button1_Command" 
              Text="Button" />

Code:

protected void Button1_Command(object sender, CommandEventArgs e)
  {
      string value = e.CommandArgument.ToString();
  }

Upvotes: 6

Joe
Joe

Reputation: 166

You would use the 'commandArgument' attribute. Here's an example on the MSDN

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

Upvotes: 4

Related Questions