5

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 ?

0

3 Answers 3

6

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();
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I Do all that but still cannot access the value (e.CommandArgument) it is empty
@Hatem - Yes! I think there is something missing. You have to update your post and add working code in it.
4

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

1 Comment

I Do all that but still cannot access the value (e.CommandArgument) it is null
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.