开发者

Anchor link disable

开发者 https://www.devze.com 2023-01-11 06:50 出处:网络
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 cl开

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 cl开发者_运维百科ick-able.How to fix it?


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.


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";


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        
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消