locked
Disable a button RRS feed

  • Question

  • User-1987327450 posted

    Hi, my problem is that I would like to disable a button after an user clicks on it, but when a add -OnClientClick- it works but then the function of the button to perform a sqlconnection is not performed

    Any idea on how to disable the button from codebehind without affecting the sqlconnection ?

    Thanks for your responses

    <asp:Button ID="ImportButton" runat="server" Text="Submit" OnclientClick="disabled=true"/><br />
    
    Code Behind
    
    Protected Sub ImportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImportButton.Click
    
    Dim Conn1 As New SqlConnection
    Dim CMD As New SqlCommand
    etc...

    Wednesday, February 26, 2014 3:50 PM

Answers

  • User281315223 posted

    This is because the OnClientClick operation is going to be triggered prior to the actual OnClick event, which triggers your server-side code. So by the time that the OnClick event is triggered, the Button is disabled and so the OnClick event isn't going to be triggered.

    You could resolve this by adding a slight delay to disabling your button as seen below :

    <!-- This will disable your button 10ms after clicking it, so that your OnClick event will be triggered and your button will be disabled until then -->
    <asp:Button ID="ImportButton" runat="server" OnClientClick="setTimeout(function(){ this.disabled=true;},10);" />

    If you wanted to disable your button through your code behind within your Button_Click event, you could use the following : 

    Protected Sub ImportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
    
         'Disable your ImportButton'
         ImportButton.Disabled = True
    
         'Another approach'
         ImportButton.Attributes("disabled") = "disabled"
    
         'Your other code here'
    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2014 4:18 PM

All replies

  • User281315223 posted

    This is because the OnClientClick operation is going to be triggered prior to the actual OnClick event, which triggers your server-side code. So by the time that the OnClick event is triggered, the Button is disabled and so the OnClick event isn't going to be triggered.

    You could resolve this by adding a slight delay to disabling your button as seen below :

    <!-- This will disable your button 10ms after clicking it, so that your OnClick event will be triggered and your button will be disabled until then -->
    <asp:Button ID="ImportButton" runat="server" OnClientClick="setTimeout(function(){ this.disabled=true;},10);" />

    If you wanted to disable your button through your code behind within your Button_Click event, you could use the following : 

    Protected Sub ImportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
    
         'Disable your ImportButton'
         ImportButton.Disabled = True
    
         'Another approach'
         ImportButton.Attributes("disabled") = "disabled"
    
         'Your other code here'
    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2014 4:18 PM
  • User-1987327450 posted

    Thanks Rion,

    This works for me, it blocks the page until the connection is done, that's very helpful bcs the user was clicking on the button a lot of times instead on waiting for response.

    <asp:Button ID="ImportButton" runat="server" OnClientClick="setTimeout(function(){ this.disabled=true;},10);" />

    About disable the button from code behind, I think it should be Enabled instead of Disabled at the first option, however, tried with those options getting an unexpected result, it goes to complete the connection without disabling the button,

    Thanks a lot!

    sorry about my English

    Wednesday, February 26, 2014 4:45 PM
  • User1208695299 posted

    Hi..
    Check this link... it may help
    http://forums.asp.net/t/1099598.aspx?How+can+disable+a+button+after+click+

    Wednesday, March 5, 2014 4:28 AM