Question

I wish to output a Button on to the screen if there is anything returned from the database. The following is what I want to achieve:

<%# IfEmpty(Eval("Link1URL"), "", "<a href="<%#Eval("Link1URL")%>" class="btn"> <%#Eval("Link1Title")%> </a>")) %>

What I am trying to say here is, If the field Link1Url is empty, then display nothing. Otherwise, display an <a> tag with the Link1URL field as the href.

This doesn't work as the inner macro: <a href="<%#Eval("Link1URL")%> messes things up for the outer IfEmpty macro.

How can I fix this issue?

Was it helpful?

Solution

I used .Net string.IsNullOrEmpty in the macro to help me next the logic:

<%# !string.IsNullOrEmpty((string)Eval("Link2URL")) ? "<a href='" + Eval("Link2URL") + "'>" + Eval("Link2Title") + "</a>" : null %>

OTHER TIPS

For more advanced scenarios you could use asp:Placeholder

Here's an example:

<asp:Placeholder runat="server" visible='<%# !String.IsNullOrEmpty(Eval("Foo").ToString()) %>' >
   <div class="foo-structure">
      ...
   </div>
</asp:Placeholder>

Conditional layout changes within transformations are discussed here.

Spoiler All of those approaches are hacky.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top