C# asp:Button OnClick 事件的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1005499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with asp:Button OnClick event
提问by Matt
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?
我究竟做错了什么?我是否不允许从 OnClick 事件向服务器传递值?
采纳答案by Jose Basilio
There are two problems here. First, the onclick event has a specific signature. It is
这里有两个问题。首先,onclick 事件有一个特定的签名。这是
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.
参数的传递可以在客户端单击事件处理程序上完成,在本例中为 OnClientClick,但不能在服务器端处理程序上完成。
回答by Kusek
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 mentionedby Jose. OnClick is bound to a function of the format void doSomething(object sender, EventArgs e)
. So your function should be of the same format.
Button 的 OnClick 是一个事件处理程序。要将函数挂接到 eventHandler,您需要遵守由 EventHandler 定义的 Contract,正如Jose所提到的。OnClick 绑定到 format 的函数void doSomething(object sender, EventArgs e)
。所以你的函数应该是相同的格式。
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;
}
回答by 7affer
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
有更简单的解决方案。您可以使用 ASP 按钮 OnCommand 事件。更多关于这里:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
回答by Amr Tareef
You Must Change your Method's Structure to be like
您必须将方法的结构更改为
public void doSomething(object sender, EventArgs e)
{
....
}
and for passing parameters you need a work around like this :
对于传递参数,您需要像这样解决:
for example if you in an Html control you may but your parameter in an attribute value (such as ID in this exapmle) and then retrieve it in the server side handler
例如,如果您在 Html 控件中,您可以将参数放在属性值中(例如本示例中的 ID),然后在服务器端处理程序中检索它
i.e to make an control changing the style color
即制作一个改变样式颜色的控件
Html :
网址:
<a href="" id="Grey" runat="server" onserverclick='ApplyStyleEvent'></a>
Code :
代码 :
protected void ApplyStyleEvent(object sender, EventArgs e)
{
Profile["SelectedStyle"] = ((HtmlControl)sender).ID;
Response.Redirect("");
}
and so on.
等等。