I'm working in C# and I need a button to become instantly disabled when a user clicks it. However, if I put as the very first line in the OnClick function
我在C#工作,我需要一个按钮,当用户点击它时立即被禁用。但是,如果我将其作为OnClick函数中的第一行
MyButton.Enabled = false;
it does nothing. The button remains enabled until it hits some sort of stop, whether it be the Catch block or the end of the function.
它什么都不做。该按钮保持启用状态,直到它达到某种停止状态,无论是Catch块还是功能结束。
I was thinking that in VB/VBA you can use DoEvents() and that allows the code to catch up to itself. However, in C# there doesn't appear to be a DoEvents method.
我想在VB / VBA中你可以使用DoEvents()并允许代码赶上自己。但是,在C#中似乎没有DoEvents方法。
This is a little different than the linked question, in that my OnClick code looks like:
这与链接的问题略有不同,因为我的OnClick代码如下所示:
OnClick="btnSubmit_Click"
and that user's OnClick code looks like:
该用户的OnClick代码如下所示:
onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"
When I tried to change my code to:
当我尝试将我的代码更改为:
OnClick="this.disabled=true; btnSubmit_Click"
I got an error.
我收到了一个错误。
Compiler Error Message: CS1041: Identifier expected; 'this' is a keyword
编译器错误消息:CS1041:预期的标识符; 'this'是一个关键字
How can I do this in a C#/asp.net environment?
我怎样才能在C#/ asp.net环境中这样做?
2 个解决方案
#1
OnClick
is a server-side event of the Button
. So you cannot write:
OnClick是Button的服务器端事件。所以你不能写:
OnClick="this.disabled=true; btnSubmit_Click"
OnClick
accepts only the method-name of the server-side event handler.
OnClick仅接受服务器端事件处理程序的方法名称。
If you want to handle the client-side button-click event to prevent that the user can click on it multiple times use OnCLientClick
:
如果要处理客户端按钮单击事件以防止用户多次单击它,请使用OnCLientClick:
OnCLientClick = "this.disabled=true;"
You also have to set UseSubmitBehaviour
to false
.
您还必须将UseSubmitBehaviour设置为false。
Read: Disable a button control during postback.
阅读:在回发期间禁用按钮控件。
#2
You will need to utilize client-side JavaScript. One way is to utilize the OnClientClick property of your asp.net button control.
您将需要使用客户端JavaScript。一种方法是利用asp.net按钮控件的OnClientClick属性。
This is server-side and will be executed on PageLoad/Postback -
这是服务器端,将在PageLoad / Postback上执行 -
MyButton.Enabled = false;
#1
OnClick
is a server-side event of the Button
. So you cannot write:
OnClick是Button的服务器端事件。所以你不能写:
OnClick="this.disabled=true; btnSubmit_Click"
OnClick
accepts only the method-name of the server-side event handler.
OnClick仅接受服务器端事件处理程序的方法名称。
If you want to handle the client-side button-click event to prevent that the user can click on it multiple times use OnCLientClick
:
如果要处理客户端按钮单击事件以防止用户多次单击它,请使用OnCLientClick:
OnCLientClick = "this.disabled=true;"
You also have to set UseSubmitBehaviour
to false
.
您还必须将UseSubmitBehaviour设置为false。
Read: Disable a button control during postback.
阅读:在回发期间禁用按钮控件。
#2
You will need to utilize client-side JavaScript. One way is to utilize the OnClientClick property of your asp.net button control.
您将需要使用客户端JavaScript。一种方法是利用asp.net按钮控件的OnClientClick属性。
This is server-side and will be executed on PageLoad/Postback -
这是服务器端,将在PageLoad / Postback上执行 -
MyButton.Enabled = false;