Bill Blankenship
Bill Blankenship

Reputation: 3366

How to stop asp.net button from being double clicked?

Ok, let me preface this question with the fact that I have read through about all the articles on this website and tried the majority of these solutions.

I have a button that has a server side event. This server side event has a (handles btnSave_click).

My first attempt was of course this. :

btnSave.enable = false

This was of course not fast enough.

Then I tried this. :

btnSave.Attributes.Add("onclick", "this.disabled=true;");

and this. :

btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, Nothing) + ";") 

This did not work, of course then I tried a bunch of different jquery and javascript via the OnClientClick event. This did not work either, but from what I could see it was due to the fact that I have that server side event firing also, I wasn't hitting my functions during stepthrough. As you can see I have spent a ton of time trying to get this to work. Hoping someone has an idea as to what I am doing incorrectly here. What I have is a very simple button and codebehind really nothing extravagant.

Here is my button. :

<asp:Button ID="btnSave" runat="server" Width="60px" Text="Save" Enabled="False"></asp:Button>

Here is the server side event header. :

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

Again any ideas are greatly appreciated. Thanks.

Upvotes: 0

Views: 1685

Answers (4)

Maysam
Maysam

Reputation: 7367

This is more clever solution: Server:

    <asp:Button ID="btnSave" runat="server" Width="60px" 
Text="Save" Enabled="False" OnClientClick="return preventDblClick()" ></asp:Button>

Client:

var dbl = 0;
function preventDblClick() {
    if (dbl < 1) {
        dbl += 1;
        return true;

    } else {
        return false;

    }
}

Upvotes: 0

Adil
Adil

Reputation: 148180

When you click on button and disable it with javascript it is disabled on client and server side does not know it is disabled on client as server side checks the viewstate for control properties. If you want to keep it disabled after being clicked and postback then you have to disable it on both client and server.

On client end

$('#<%= btnSave.ClientID %>').click(function() {
         this.disabled = true;
    });

On server end

btnSave.enable = false

Upvotes: 1

Giovanni B
Giovanni B

Reputation: 1032

Something like this should work:

 $('#yourButtonId').click(function() {
         $(this).attr('disabled', 'disabled');
    });

Upvotes: 1

Paul Fleming
Paul Fleming

Reputation: 24526

Something like this?

client:

var buttonPressed = false;

server:

btnSave.OnClientClick = "if (buttonPressed) return false else buttonPressed = true; return true;" 

Upvotes: 1

Related Questions