Sjoerd
Sjoerd

Reputation: 75619

Optional ASP Hyperlink

I have some company names within a Repeater, and each of the companies may or may not have a link.

<asp:HyperLink runat="server" Visible="false">
    <asp:Literal runat="server" ID="CompanyName" />
</asp:HyperLink>

If I set the NavigateUrl to null, it still renders the <a> tag in the HTML. If I set it to Visible=False, it does not render the company name. Is it possible to remove the HyperLink but keep the company name if there is no NavigateUrl?

Upvotes: 1

Views: 479

Answers (2)

Tim B James
Tim B James

Reputation: 20364

There are a couple of ways you could do this. One would be to build up the html using a code block

<%#IIF(CompanyName <> "", "<a href='" & Eval("CompanySiteUrl") & "'>" & Eval("CompanyName") & "</a>", Eval("CompanyName"))%>

Or you could create a public method that you can call and then just build up the html in the method

<%#BuildCompanyUrl(Eval("CopmanySiteUrl"), Eval("CompanyName"))%>

Public Function BuildCompanyUrl(ByVal CompanySiteUrl as string, ByVal Copmanyname as string) As String
     ' build up the logic here and return the html
    return "<a href="......."
End Function

Upvotes: 0

IUnknown
IUnknown

Reputation: 22468

<asp:Literal runat="server" Text='<%# Eval("CompanyName") %>' Visible='<%# string.IsNullOrEmpty(Eval("CompanySiteUrl") as string) %>' />
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("CompanySiteUrl") %>' Visible='<%# !string.IsNullOrEmpty(Eval("CompanySiteUrl") as string) %>'>
    <%# Eval("CompanyName") %>
</asp:HyperLink>

Upvotes: 2

Related Questions