Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Disable a hyperlink

I have the code below that works in IE but not in Chrome

myherplink.Disabled = true;

Open in new window


I found this link and if you scroll down (the last solution), it has a CSS. I tried that but now, it works in Chrome and the link is disabled, user can't click on it but in IE, it still works..

myherplink.Attributes.Add("style", "pointer-events: none;cursor: default;");

Open in new window


Should I have both lines of code to make it works in both IE and Chrome or is there another way of doing this?

http://forums.asp.net/p/1895263/5355741.aspx/1?Re+hyperlink+disabled+works+in+IE+but+not+in+Chrome+or+Firefox
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

try this,

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function DisableField() {
            hypLoadFav = document.getElementById("HyperLink1");
            hypLoadFav.setAttribute("onclick", "return false;");
            hypLoadFav.removeAttribute('href');
            return false;
        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" href="123.aspx">HyperLink</asp:HyperLink><br />
        
        <input type="button" id="adsf" onclick="DisableField();" value="disabled"/>
    </div>
    </form>
</body>
</html>

Open in new window

Another approach,with tooltip
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
    .disabled
    {
        cursor:pointer;
        text-decoration:underline;
        color:Gray;
        }
    </style>
    <script>
        function DisableField() {
            hypLoadFav = document.getElementById("HyperLink1");
            //hypLoadFav.setAttribute("onclick", "return false;");
            hypLoadFav.removeAttribute('href');
            hypLoadFav.className = "disabled";
            hypLoadFav.title = "disabled";
            return false;
        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" href="123.aspx">HyperLink</asp:HyperLink><br />
        
        <input type="button" id="adsf" onclick="DisableField();" value="disabled"/>
    </div>
    </form>
</body>
</html>

Open in new window

Avatar of Camillia

ASKER

Is there anyway I can do that in code behind because I need to check for some flags from the database..I think I need to somehow call the function in code behind and add it to the attribute...something like that...in the line below...

myherplink.Attributes.Add("style", "pointer-events: none;cursor: default;");
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial