0

I have a template field in a gridview with a label whose text I want to alter depending on an if statement. The Following of course does not work but this is what i want to accomplish.

 <%if Eval("Address") != ""%>
    <%{ %>
         <ItemTemplate>
          <asp:Label ID="Label18" nowrap="nowrap" runat="server" Text='<%# Eval("Address") + "<br>" + Eval("City") + "," + Eval("State") + " " + Eval("Zip1") + " " + Eval("Zip2") %>'></asp:Label>
        </ItemTemplate>
  <%} %>

How can I achieve this?

3 Answers 3

0

What about this?

<ItemTemplate>
<asp:Label ID="Label18" nowrap="nowrap" runat="server" Text='<%# Eval("Address") != String.Empty ? Eval("Address") + "<br>" + Eval("City") + "," + Eval("State") + " " + Eval("Zip1") + " " + Eval("Zip2") : String.Empty %>'></asp:Label>
</ItemTemplate>
3
  • i really like this idea. But it still goes through the code. I tried String.IsnullorEmpty but that didn't work either
    – Eric
    Dec 4, 2009 at 16:36
  • I've never seen this approach - but how can i use it if the value is null?
    – Eric
    Dec 4, 2009 at 16:37
  • String.IsNullOrEmpty(Eval("Address")) Dec 4, 2009 at 16:52
0

You could have a DisplayAddress property on your datasource - which is either the Address concatenation that you want or null/blank.

Not knowing how your binding your grid, I can't advise on the best way to do this.

0

Consider having a placeholder in your itemTemplate:

<ItemTemplate>
   <asp:PlaceHolder ID="phAddress" runat="server" />
</ItemTemplate>

For your gridview, have an OnRowDataBound event where you can construct a new asp:Label or plain HTML. Use the e.Row.DataItem to find the address details that you need.

Then you can assign that label or HTML string to phAddress.

This related answer may help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.