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

I have a repeater that will output a number of elements:

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

But some elements will not have a linked link, so I do not want them to be viewable. I tried to make it runat=server HtmlAnchor , and set htmlAnchor.Disabled = true for the elements in fact there should not be a link, but they can still be clicked (this just makes the text gray)

I know how to do this in the old days:

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

But this is dirty mixing code and html ASP path. How is ASP.NET?

+4
source share
1 answer

Use the <asp: HyperLink> control, which usually displays text if no link is provided.


Edited to include an 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 display in html independently, but if the NavigateUrl attribute is an empty string, then there will be no href at all, and every browser I have ever used displays text in the same way as scrolls (so pay attention to user styles on <a>).

+10
source

Source: https://habr.com/ru/post/1277578/


All Articles