Reputation: 15607
I have an anchor link like:<a id="linkOwner" runat="server"></a>
In my codebehind I am disabling it based on some condittions like:linkOwner.Disabled = true; But still the link is click-able.How to fix it?
Upvotes: 1
Views: 4246
Reputation: 1076
Disable anchor button by calling javascript void function and call
another doAction
function which will hanle your condition.
HTML implementation:
<a href='javascript:void(0);' onclick="doAction()">some text</a>
Javascript implementation:
function doAction() {
if ( condition here ) {
// do X
} else {
// do Y action
}
}
Upvotes: 0
Reputation: 2801
There are two solutions:
Change the anchor tag to an <asp:HyperLink>
then you can set the Enabled
property as you see fit.
You need to add an attribute to the control as in
linkOwner.Attributes["disabled"] = "disabled";
Upvotes: 1
Reputation: 10675
If you use an ASP LinkButton control I think you can just disable it on the server side and it'll properly disable it on the client. Not positive on that though. Another method is to use javascript. In the past I have used jQuery to add a click event to the disabled anchor with a empty event that returns false. Something like:
function disabler(){ return false; }
$('#linkOwner').click(disabler);
//to reactive the link
$('#linkOwner').unbind('click', disabler);
The return false lets jQuery know not to bubble up the event.
Upvotes: 1