views:

2556

answers:

4

Hi,

Here is my button

<asp:Button ID="myButton" Text="Click Me" OnClick="doSomething(10)" runat="server" />

Here is the server function

public void doSomething(int num)
{
    int someOtherNum = 10 + num;
}

When I try to compile the code I get the error "Method Name Expected" for the line:

<asp:Button ID="myButton" Text="Click Me" OnClick="doSomething(10)" runat="server" />

What am I doing wrong? Am I not allowed to pass values to the server from an OnClick event?

+8  A: 

There are two problems here. First, the onclick event has a specific signature. It is

MethodName(object sender, EventArgs e);

Second, in the markup, you need to pass the Method name only with no parentheses or params.

<asp:Button ID="myButton" Text="Click Me" OnClick="doSomething" runat="server" />

Then change your codebehind as follows:

public void doSomething(object sender, EventArgs e)
{
    ....
}

The passing of parameters can done on a client side click event handler, in this case OnClientClick, but not on the server side handler.

Jose Basilio
So there's absolutely no way to pass that method a number as long as it's a server side method?
Matt
That's correct. You can however, read values from other input fields like textboxes or even hidden controls from inside your event handler and use that for your input calculations.
Jose Basilio
+1  A: 

OnClick of the Button is an Event Handler. To hook a function to an eventHandler you need to obey the Contract that was defined by the EventHandler, as mentioned by Jose. OnClick is bound to Function of the Format void doSomething(object sender, EventArgs e). So you function should be of same thing.

I am unsure why would want to pass a parameter to the Event Handler. If you want to take some manuplation you need to do that using some other control.

<asp:TextBox ID="txtNumber" runat="server" /><asp:Button ID="myButton" Text="Click Me" OnClick="doSomething" runat="server" />

And in the Code

public void doSomething(object sender, EventArgs e)

{ int AnotherNumber= Int32.Parse(txtNumber.Text)+10; }

Kusek
A: 

There is simpler solution. You could use ASP button OnCommand event. More about here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx

7affer
A: 

The percentage of calories from is:

<td width="50%"><Input Type="Reset"></td>

This is my code, I need to locate the Input Tag for the Calculate button , Insert the onclick event handler to call the FatCal() user-defined function. Can anyone help me?

Marylyn
I am calculating a percentage and formatting the result as a percentage. <tr> <td width="50%"><p align="right">The percentage of calories from is: </td> <td width="50%"><Input Type="text" Name="FatPerc" value=" "></td> </tr> <tr> <td width="50%"><p align="right"><Input Type="Button" Value="Calculate"> </td> <td width="50%"><Input Type="Reset"></td> </tr>I neeed to calculate the number of calories from fat alone and the percentage of the total calories in attable.
Marylyn