5

In my project I need to disable a hyperlink based on some condition. So how can I do this from code behind using C#?

onof
  • 17,167
  • 7
  • 49
  • 85
ANP
  • 15,287
  • 22
  • 58
  • 79
  • Im assuming you mean a client-side anchor tag? If that's the case, you can't. You'll have to do it with javascript. (or make the anchor tag a server-side asp.net control) – RPM1984 Aug 05 '10 at 10:20
  • 1
    Also, is this a WebForms based or an MVC based application? – Manfred Aug 05 '10 at 10:21

3 Answers3

11

in your aspx, add runat="server" attribute to the tag:

<a id="myHyperLink" runat="server">...</a>

in Page_load method:

if( condition )
    myHyperLink.Enabled = false;
onof
  • 17,167
  • 7
  • 49
  • 85
0

in your aspx, add runat="server" and id attribute to the tag:

in Page_load method: if(condition) ep_sms.HRef = "#";

0

myHyperLink.Enabled = false; does not work with <a> links

Instead use aria-disabled="true"

In your method add this:

myHyperLink.Attributes.Add("aria-disabled", "true");

links:

https://a11y-guidelines.orange.com/en/articles/disable-elements/#:~:text=It%20is%20still%20possible%20to,is%20indicated%20as%20being%20disabled

How to add custom attributes to ASP.NET controls