?
Solved

Disable a hyperlink

Posted on 2013-05-28
4
Medium Priority
?
263 Views
Last Modified: 2013-06-01
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
0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 39203382
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

0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 39203397
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

0
 
LVL 7

Author Comment

by:Camillia
ID: 39204296
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;");
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 2000 total points
ID: 39205206
I guess , you are looking to disable the hyperlink when certain condition fails from database . For that, you dont need javascript  process at all. If you simply remove href from the hyperlink the link will be disabled in all the browser .

myherplink.Attributes.Remove("href");
        myherplink.Attributes.Add("style", "cursor:pointer;text-decoration:underline;color:Gray;");

Open in new window


Complete Code.

<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">Disabled Link </asp:HyperLink><br />
        
    </div>
    </form>
</body>
</html>

Open in new window


    protected void Page_Load(object sender, EventArgs e)
    {
       
HyperLink1.Attributes.Remove("href");
        HyperLink1.Attributes.Add("style", "cursor:pointer;text-decoration:underline;color:Gray;");
    }

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question