Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: How to convert <A> or HtmlAnchor to static text?

Tags:

html

asp.net

i have a repeater that will output a series of items:

<asp:repeater ... runat="Server">
   <itemtemplate>
      <a href="<%# GetItemLink(...) %>"><%# GetItemText %></a>
   <itemtemplate>
<asp:repeater>

But some items will not have an associated link, so i don't want them to be clickable. i tried making it a runat=server HtmlAnchor, and set the htmlAnchor.Disabled = true for the items are should not actually have a link - but they can still be clicked (it just makes the text gray)

i know how i'd do it in the olden days:

<% If IsLink Then %>
   <A href="<% =GetItemLink%">
<% End If %>
   <% =GetItemText %>
<% If IsLink Then %>
   </A>
<% End If %>

But that's the messy mixing code and html ASP way. What's the ASP.NET way?

like image 262
Ian Boyd Avatar asked Dec 30 '22 09:12

Ian Boyd


1 Answers

Use an <asp:HyperLink > control, which displays the text normally if no link is supplied.


Edited to include example:

<asp:repeater ... runat="Server">
   <itemtemplate>
      <asp:HyperLink ... runat="server" NavigateUrl="<%# GetItemLink(...) %>"> <%# GetItemText %></asp:HyperLink>
   <itemtemplate>
<asp:repeater>

In the above example, the anchor tag will be rendered to html regardless, but if the NavigateUrl attribute is an empty string, there will be no href at all and every browser I've ever used renders the text in a manner similar to spans (so look out for custom styles on <a >'s).

like image 192
Joel Coehoorn Avatar answered Jan 01 '23 23:01

Joel Coehoorn