tags:

views:

422

answers:

1

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?

+10  A: 

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).

Joel Coehoorn