最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery Asp.net button disable - Stack Overflow

programmeradmin0浏览0评论

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 ?

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 ?

Share Improve this question edited May 25, 2010 at 14:57 Chicharito asked May 25, 2010 at 14:39 ChicharitoChicharito 1,4408 gold badges31 silver badges49 bronze badges 2
  • You need to know ClientID of your button in JS. It is not the same as ID, but something like ctl00_Content1_btnTest, if you use masterpage. – Serge S. Commented May 25, 2010 at 14:57
  • The problem is that you have to set disabled="disabled" attribute instead of disabled="true". – Serge S. Commented May 25, 2010 at 15:01
Add a comment  | 

6 Answers 6

Reset to default 8

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

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.

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

I can fix your problems:$(".classButton").prop('disabled','disabled'); and remove disabled: $(".classButton").prop('disabled', '');

Wouldn't you just need to do the following:

btnTest.Enabled = False;

in the code-behind file? This will cause a postback but it should work.

It wouldn't work because the generated HTML id is different than the ASP.NET id.
So btnTest will be rendered as another Id.

A quick dirty way is to to run the page, view the HTML source and locate the button's generated Id and pass it as an arugment in the jQuery function.

A better way is to generate the jQuery function through code behind:

Literal1.Text = "$('[id$=" + btnTest.ClientId + "]').click(function(
{$(this).attr('disabled', 'disabled');});";

Edit: Also I couldn't help but realize that your OnClick attribute should point to btnTest_Click and not btn_Click

发布评论

评论列表(0)

  1. 暂无评论