Solved

Disable a hyperlink

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now