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
Solved

Disable a hyperlink

Posted on 2013-05-28
4
258 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
  • 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 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
encoding an href string 2 24
Easy filter aspnet 2 24
SSRS Deployment problem 5 64
asp.net mvc , views, hidden values ? 2 16
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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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