开发者

Optional ASP Hyperlink

开发者 https://www.devze.com 2023-03-08 14:10 出处:网络
I have s开发者_开发知识库ome company names within a Repeater, and each of the companies may or may not have a link.

I have s开发者_开发知识库ome 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?


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消