<p>Is it possible to prevent an asp.net Hyperlink control from linking, i.e. so that it appears as a label, without actually having to replace the control with a label? Maybe using CSS or setting an attribute?</p>

<p>I know that marking it as disabled works but then it gets displayed differently (greyed out).</p>

<p>To clarify my point, I have a list of user names at the top of my page which are built dynamically using a user control. Most of the time these names are linkable to an email page. However if the user has been disabled the name is displayed in grey but currently still links to the email page. I want these disabled users to not link.</p>

<p>I know that really I should be replacing them with a label but this does not seem quite as elegant as just removing the linking ability usings CSS say (if thats possible). They are already displayed in a different colour so its obvious that they are disabled users. I just need to switch off the link.</p>

[{'answer_id': 49358, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>If you merely want to modify the appearance of the link so as not to look like a link, you can set the CSS for your "a" tags to not have underlines:</p>\n\n<pre><code>a: link, visited, hover, active {\n    text-decoration: none;\n}\n</code></pre>\n\n<p>Though I would advise against including "hover" here because there will be no other way to know that it\'s a link.</p>\n\n<p>Anyway I agree with @pilif here, this looks like a usability disaster waiting to happen.</p>\n'}, {'answer_id': 49362, 'author': 'Slartibartfast', 'author_id': 4433, 'author_profile': 'https://Stackoverflow.com/users/4433', 'pm_score': 2, 'selected': False, 'text': '<p>This should work:  </p>\n\n<pre><code>onclick="return false;"  \n</code></pre>\n\n<p>if not, you could change href to "#" also. Making it appear as a rest of text is css, e.g. displaying arrow instead of hand is:  </p>\n\n<pre><code>a.dummy {  \n    cursor:default;  \n}  \n</code></pre>\n'}, {'answer_id': 49363, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m curious on what it is you which to accomplish with that. Why use a link at all?</p>\n\n<p>Is it just for the formatting? In that case, just use a &lt;span&gt; in HTML and use stylesheets to make the format match the links.</p>\n\n<p>Or you use the link and attach an onClick-Event where you "return false;" which will make the browser not do the navigation - if JS is enabled.</p>\n\n<p>But: Isn\'t that terribly confusing for your users? Why create something that looks like a link but does nothing?</p>\n\n<p>Can you provide more details? I have this feeling that you are trying to solve a bigger problem which has a way better solution than to cripple a link :-)</p>\n'}, {'answer_id': 49364, 'author': 'Andrew Johnson', 'author_id': 5109, 'author_profile': 'https://Stackoverflow.com/users/5109', 'pm_score': 2, 'selected': False, 'text': '<p>If you mean to stop the link from activating, the usual way is to link to "javascript:void(0);", i.e.:  </p>\n\n<p>&lt;a href="javascript:void(0);"&gt;foo&lt;/a&gt;</p>\n'}, {'answer_id': 49380, 'author': 'Radu094', 'author_id': 3263, 'author_profile': 'https://Stackoverflow.com/users/3263', 'pm_score': 2, 'selected': False, 'text': '<p>A Hyperlink control will render as a "a" "/a" tag no matter what settings you do. You can customize a CSS class to make the link look like a normal label.</p>\n\n<p>Alternatively you can build a custom control that inherits from System.Web.UI.WebControls.HyperLink, and override the Render method </p>\n\n<pre><code>protected override void Render(HtmlTextWriter writer)\n        {\n            if (Enabled)\n                base.Render(writer);\n            else\n            {\n                writer.RenderBeginTag(HtmlTextWriterTag.Span);\n                writer.Write(Text);\n                writer.RenderEndTag(HtmlTextWriterTag.Span);\n            }\n        }\n\n     }\n</code></pre>\n\n<p>Could be a bit overkill, but it will work for your requirements. </p>\n\n<p>Plus I find is usefull to have a base asp:CustomHyperlink asp:CustomButton classes in my project files. Makes it easier to define custom behaviour throughout the project.</p>\n'}, {'answer_id': 49381, 'author': 'NakedBrunch', 'author_id': 3742, 'author_profile': 'https://Stackoverflow.com/users/3742', 'pm_score': 4, 'selected': True, 'text': '<p>This sounds like a job for JQuery.  Just give a specific class name to all of the HyperLink controls that you want the URLs removed and then apply the following JQuery snippet to the bottom of your page:</p>\n\n<pre><code>$(document).ready(function() {\n    $(\'a.NoLink\').removeAttr(\'href\')\n});\n</code></pre>\n\n<p>All of the HyperLink controls with the class name "NoLink" will automatically have all of their URLs removed and the link will appear to be nothing more than text.</p>\n\n<p>A single line of JQuery can solve your problem.</p>\n'}, {'answer_id': 49412, 'author': 'Simon Keep', 'author_id': 1127460, 'author_profile': 'https://Stackoverflow.com/users/1127460', 'pm_score': 2, 'selected': False, 'text': "<p>Thanks for all the input, it looks like the short answer is 'No you can't (well not nicely anyway)', so I'll have to do it the hard way and add the conditional code.</p>\n"}, {'answer_id': 50815, 'author': 'Matthew M. Osborn', 'author_id': 5235, 'author_profile': 'https://Stackoverflow.com/users/5235', 'pm_score': 0, 'selected': False, 'text': "<p>If you are using databind in asp.net handle the databinding event and just don't set the NavigateUrl if that users is disabled.</p>\n"}, {'answer_id': 50818, 'author': 'John Rutherford', 'author_id': 3880, 'author_profile': 'https://Stackoverflow.com/users/3880', 'pm_score': 0, 'selected': False, 'text': "<p>Have you tried just <strong>not</strong> setting the NavigateUrl property?  If this isn't set, it may just render as a span.</p>\n"}, {'answer_id': 47861432, 'author': 'Norman Bird', 'author_id': 1461309, 'author_profile': 'https://Stackoverflow.com/users/1461309', 'pm_score': 0, 'selected': False, 'text': '<p>.fusion-link-wrapper { pointer-events: none; }</p>\n'}, {'answer_id': 47924576, 'author': 'Hasan Ali', 'author_id': 8744304, 'author_profile': 'https://Stackoverflow.com/users/8744304', 'pm_score': 0, 'selected': False, 'text': '<p>Another solution is apply this class on your hyperlink.</p>\n\n<pre><code>.avoid-clicks {\n  pointer-events: none;\n}\n</code></pre>\n'}, {'answer_id': 70901572, 'author': 'Jay Irvine', 'author_id': 4756306, 'author_profile': 'https://Stackoverflow.com/users/4756306', 'pm_score': 0, 'selected': False, 'text': "<p>CSS solution to make  tags with no href (which is what asp:HyperLink will produce if NavigateURL is bound to null/empty string) visually indistinguishable from the surrounding text:</p>\n<pre><code>a:not([href]), a:not([href]):hover, a:not([href]):active, a:not([href]):visited {\n    text-decoration: inherit !important;\n    color: inherit !important;\n    cursor: inherit !important;\n}\n</code></pre>\n<p>Unfortunately, this won't tell screen readers not to read it out as a link - though without an href, it's not clickable, so I'm hoping it already won't be identified as such.  I haven't had the chance to test it though.</p>\n<p>(If you also want to do the same to links with href=&quot;&quot;, as well as those missing an href, you would need to add <code>pointer-events:none</code> as well, since otherwise an empty href will reload the page. This definitely leaves screen readers still treating it as a link, though.)</p>\n<p>In the OP's use case, if you still have the href being populated from the database but have a boolean value that indicates whether the link should be a 'real' link or not, you should use that to disable the link, and add <code>a:disabled</code> to the selector list above. Then disabled links will also look like plain text rather than a greyed-out link. (Disabling the link will also provide that information to screen readers, so that's better than just using pointer-events: none and a class.)</p>\n<p>A note of caution - if you add these sorts of rules globally rather than for a specific page, remember to watch out for cases where an  tag has no (valid) href, but you <em>are</em> providing a click handler - you still need those to look/act like links.</p>\n"}]
['https://Stackoverflow.com/questions/49346', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1127460/']