Dan Wier Dan Wier - 7 months ago 36
Vb.net Question

concatenate if inline eval is not nothing

I have a data list with an item template that displays two values with a space between them. The issue is, when the first value is empty, there is a leading empty space. What I would like to do is only concatenate the space if there is a value in the first eval.

<ItemTemplate>
<%#Container.DataItem("CustomText")%>&nbsp;
<%#Container.DataItem("ServiceDescription")%>
</ItemTemplate>


I want something like:

<ItemTemplate>
<%#" " + Container.DataItem("CustomText")%>&nbsp;
<%#Container.DataItem("ServiceDescription")%>
</ItemTemplate>


Where it only process any of the first <%# %> if there is a value for CustomText.

Make sense? I like it to have the leading &NBSP; because it allows it to offset the start of multi line descriptions.

Answer

You can try this:

<%# " " + IIf(IsDBNull(Container.DataItem("CustomText")) OrElse String.IsNullOrEmpty(Container.DataItem("CustomText")), String.Empty, String.Format("{0}&nbsp;", Container.DataItem("CustomText"))) & Container.DataItem("ServiceDescription") %>