Instantly disable Button on click

此生再无相见时 提交于 2019-12-13 13:29:51

问题


I'd like to instantly disable a Button after clicking it, so a User can't click it twice in short succession and fire the OnClick_Event twice in a row. btn.Enabled = false doesn't seem to do the trick instantly. Is there any other way to accomplish this?

Thanks,

Dennis


回答1:


What you are doing is disabling it after a post back therefore your button will be disabled in the page that's rendered when the browser receives the response.

Do it on the client-side with JavaScript instead:

var button = document.getElementById('yourButton');
button.disabled = true;

If you're facing issues with posting back to the server, check out this article: How to disable an ASP.NET button when clicked.




回答2:


function disable()
{
var button = document.getElementById('<%= Button1.ClientID %>');
button.disabled = true;
}

<asp:Button ID="Button1" OnClientClick="disable();">



回答3:


you can achieve this via javascript

function Disable(btn)
{
btn.disabled = true;
}

and add this to your button OnClientClick="javascript:return Disable(this);"




回答4:


You can handled it simply by this code.Suppose, here is a button named btnAdd.When you click this button it will be disable due to execute it's corresponding code.I mean due to post back it will be disabled.

if (!IsPostBack)
    {
     btnAdd.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnAdd).ToString());
    }



回答5:


Write the btn.Enabled= false at the end of all the functionality in the Onclick Event of the Button. It will do the Required action.




回答6:


Disabling the button has raised on problem. You won't get the server side event fired. So better seek the alternative by hiding the button (and yes displaying some friendly error message 'working', 'processing' etc.). The work out on how to disable asp.net button on postback would help. Thanks.




回答7:


YourButton.Enabled = false;

if(!YourButton.Enabled)
{
\\Your Code Here
YourButton.Enabled = true;
}

Hope this helps ;)



来源:https://stackoverflow.com/questions/5868042/instantly-disable-button-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!