Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to click event method using c#

Tags:

c#

asp.net

events

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 ?

like image 903
Hatem Avatar asked Dec 16 '11 05:12

Hatem


People also ask

How do you pass arguments to onclick functions in Blazor?

You can use a lambda expression to pass multiple arguments to an onclick event.

How do you get the target element onclick event in Blazor?

Blazor does not have support to manipulate DOM elements directly, but we can still achieve it by using JavaScript interop. By creating a reference to an element, we can send it as a parameter to the JavaScript method via interop.

How do you pass parameters in Blazor?

By using Blazor route parameters, you can pass values from one page to another page. Use NavigationManager to pass a value and route the parameter to another page. Follow this example to achieve passing values from one page to another. Get the passed Page1 parameter value in Page2.


2 Answers

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();
  }
like image 114
KV Prajapati Avatar answered Nov 08 '22 13:11

KV Prajapati


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

like image 30
Joe Avatar answered Nov 08 '22 12:11

Joe