Search code examples
c#asp.netfirefoxwebformslinkbutton

ASP.NET linkbutton enabled=false does not work in Firefox


I want to disable an ASP.NET linkbutton. What I want is a solution where browsers won't let me click on the linkbutton or at least where the associated action is not executed.

I do the following on the HTML markup using WebForms:

 <asp:LinkButton
       id="link1"
       text="Some button"
       runat="server"
       Enabled ="false"
 />

I trigger the link button action using jQuery events:

$('#link1').click(function (evt) {
    // Do something here
    return false;
});
  • In IE8 it is disabled and does not allow click
  • In Chrome it's not disabled but id does not allow click
  • In Firefox 41.0.1 I can still click the link button and perform the action

The result is the same if I disable it in code behind:

this.link1.Enabled = false;

Solution

  • Making a Linkbutton disabled does not prevent it from being clicked. In my test a disabled linkbutton rendered like this:

    <a class="aspNetDisabled">test</a>
    

    jQuery will happily attach a click event to that anchor tag. You need to either prevent the event from triggering, or change the code so that it only performs your actions when the button is enabled.

    Here's a possible work-around which will only execute your js if the button is enabled:

    $('#link1').click(function (evt) {
        if('<%=link1.Enabled%>' === 'True') {
          // Do something here
        }
        return false;
    });
    

    Even if the client triggers the disabled button, the if statement will cause Do something here to not run.