按钮的OnClick事件:与ASP问题 [英] Problems with asp:Button OnClick event
问题描述
下面是我的按钮
<asp:Button ID="myButton" Text="Click Me" OnClick="doSomething(10)" runat="server" />
下面是服务器的功能
public void doSomething(int num)
{
int someOtherNum = 10 + num;
}
当我尝试编译code,我得到该行的错误预期的方法名称
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" />
我是什么做错了吗?我不能让值从一个onclick事件传递给服务器?
What am I doing wrong? Am I not allowed to pass values to the server from an OnClick event?
推荐答案
这里有两个问题。首先,onclick事件有一个特定的签名。这是
There are two problems here. First, the onclick event has a specific signature. It is
MethodName(object sender, EventArgs e);
二,在标记,你只需要使用没有括号或PARAMS传递的方法名。
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" />
然后,修改codebehind如下:
Then change your codebehind as follows:
public void doSomething(object sender, EventArgs e)
{
....
}
的参数的通过可以在客户端的点击事件处理程序完成的,在这种情况下的OnClientClick,但不是在服务器端的处理程序。
The passing of parameters can done on a client side click event handler, in this case OnClientClick, but not on the server side handler.
这篇关于按钮的OnClick事件:与ASP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!