admin管理员组

文章数量:1292123

I have a LinkButton (initially disabled) that needs to be enabled or disabled using jquery, but it seems like disabling the button with the method I'm using removes the href attribute, so when I re-enable it, the button looks enabled, but it's not clickable.

<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" ClientIDMode="Static" />

Disable:
$('#lbSave').attr('disabled', 'disabled');

Enable:
$('#lbSave').removeAttr('disabled');

What's the correct way to enable/disable LinkButtons?

I have a LinkButton (initially disabled) that needs to be enabled or disabled using jquery, but it seems like disabling the button with the method I'm using removes the href attribute, so when I re-enable it, the button looks enabled, but it's not clickable.

<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" ClientIDMode="Static" />

Disable:
$('#lbSave').attr('disabled', 'disabled');

Enable:
$('#lbSave').removeAttr('disabled');

What's the correct way to enable/disable LinkButtons?

Share Improve this question edited Oct 31, 2012 at 17:27 Prabhu asked Oct 31, 2012 at 17:09 PrabhuPrabhu 13.4k34 gold badges133 silver badges215 bronze badges 1
  • FWIW disabling an anchor client-side is not the same as disabling it server side (the rendered html is different via server side disabling) you cannot reliably block the anchor action via setting the disabled attribute client side and I would remend using a different type of html element or removing the href entirely from the anchor. – Quintin Robinson Commented Oct 31, 2012 at 17:14
Add a ment  | 

4 Answers 4

Reset to default 5

The problem is that the id of the control in the client browser is not lbSave but WebForms will mangle it into something like ctl00_nameofanynestedaspnetcontainercontrols_morenestedcontainernames_lbSave.

The workaround as suggested by Praveen is, don't try to find the control by id but instead give it a unique classname - which webforms won't mangle - and voila problem solved.

The alternative is to get asp to plug the correct id in for you in your markup:

Enable: $('#<%= lbSave.ClientID %>').attr('disabled', '');

Disable: $('#<%= lbSave.ClientID %>').removeAttr('disabled');

(If you're using Asp.Net 4 WebForms this page http://weblogs.asp/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx gives you an alternative simpler solution).

But if you're doing new projects in Asp.Net 4, you really really want to use Asp.Net MVC and all this kind of problem goes away.

You can do something like below:

<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" CssClass="lbSave" />

Disable:

$(".lbSave").attr("disabled", "disabled");

Enable:

$(".lbSave").removeAttr("disabled");

Hope this Helps!!

try this link disable a hyperlink using jQuery

so you can try

$('#<%= lbSave.ClientID %>').bind('click', false);

to enable

$('#<%= lbSave.ClientID %>').unbind('click', false);

Setting Enabled=False on an ASP.NET LinkButton removes the href attribute, so jquery can never enable it back. So here's a solution that works for me:

In javascript:
$(document).ready(function() {
  _href = $('#lbSave').attr('href');
  $('#lbSave').attr('disabled', 'disabled');
  $('#lbSave').removeAttr('href');
});

Disable:
$('#lbSave').attr('disabled', 'disabled');
$('#lbSave').removeAttr('href');

Enable:
$('#lbSave').removeAttr('disabled');
$('#lbSave').attr('href', _href);

本文标签: javascriptToggling enableddisabled state of a LinkButton using jqueryStack Overflow