ANP
ANP

Reputation: 15607

Anchor link disable

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

Answers (3)

Shiv Kumar Sah
Shiv Kumar Sah

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

Philip Smith
Philip Smith

Reputation: 2801

There are two solutions:

  1. Change the anchor tag to an <asp:HyperLink> then you can set the Enabled property as you see fit.

  2. You need to add an attribute to the control as in

linkOwner.Attributes["disabled"] = "disabled";

Upvotes: 1

Sean Copenhaver
Sean Copenhaver

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

Related Questions