开发者

Instantly disable Button on click

开发者 https://www.devze.com 2023-03-02 02:45 出处:网络
I\'d like to instantly disable a Button after clicking it, so a User can\'t click it twice in short succes开发者_如何学编程sion and fire the OnClick_Event twice in a row. btn.Enabled = false doesn\'t

I'd like to instantly disable a Button after clicking it, so a User can't click it twice in short succes开发者_如何学编程sion 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


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.


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

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


you can achieve this via javascript

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

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


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());
    }


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.


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.


YourButton.Enabled = false;

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

Hope this helps ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消