Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Asp.net button disable

html code:

<asp:Button runat="server" ID="btnTest" Text="Test" OnClick="btnTest_Click" />

Jquery Code:

$('[id$=btnTest]').click(function(){
   $('[id$=btnTest]').attr('disabled', 'true');
});

CodeBehind:

protected void btnTest_Click(object sender, EventArgs e)
{
//here not come.
}

Code Behind btnTest event not work ?

like image 776
Chicharito Avatar asked May 25 '10 14:05

Chicharito


3 Answers

I think that making the button disabled in the click event handler is preventing the postback. Try executing the disabling code after some time:

$('[id$=btnTest]').click(function(){
    var button = this;   

    setTimeout(function() {
        $(button).attr('disabled', 'disabled');
    }, 100);
});
like image 67
Atanas Korchev Avatar answered Oct 16 '22 23:10

Atanas Korchev


Try to use jQuery class selector:

  1. Add CssClass="MyButton" to your ASP.NET button;
  2. Use this selector in jQuery
  3. Set disabled="disabled" attribute on click

jQuery:

$('button.MyButton').click(function(){ 
  $(this).attr('disabled', 'disabled');
});
like image 20
Serge S. Avatar answered Oct 17 '22 00:10

Serge S.


The sample code is using the ends-with selector. There is no mistake in selector. you just need to change the code like this

$('[id$=btnTest]').click(function () {
       $('[id$=btnTest]').attr('disabled', true);
});

I have tested this and works fine without any issues.

like image 23
Elangovan Avatar answered Oct 17 '22 01:10

Elangovan