Solved

Disable a hyperlink

Posted on 2013-05-28
4
260 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 44
C# LINQ ForEach() question 6 87
Problem to copy file 14 52
How to set focus on a dynamic control 18 26
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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