If condition in aspx.page not code behind

asp.net newbie

Not sure if this is a good way. I’m trying to build a contact page and spray the page with contact info from sql database. The repeater control contains the record set. I want to put a condition in my aspx page that will check if the address2 has data and if so wrap the address 2data around a paragraph. i’m not sure on the proper syntax when coding in the aspx page so any help would be appreciate? Also, im not sure if this is the best way so I’m open to suggestions.

Here’s the code I highlighted the problem in red.

<asp:Repeater ID=“contactData” runat=“server”>
<ItemTemplate>
<p><%#DataBinder.Eval(Container.DataItem, “display_name”)%></p>
<p><%#DataBinder.Eval(Container.DataItem, “first_name”)%> <%#DataBinder.Eval(Container.DataItem, “last_name”)%></p>
<p><%#DataBinder.Eval(Container.DataItem, “address1”)%></p>
<%if (DataBinder.Eval(Container.DataItem, “address2”))%><p><%#DataBinder.Eval(Container.DataItem, “address2”)%></p>
</ItemTemplate>
</asp:Repeater>

Very doable–what sort of object is the data coming in as? That kind of determines syntax.

The data is a sql recordSet that is being binded to the repeater object.

Yes, it is possible, but you cannot use and if statement in a repeater for some reason, but you can do an inline if statement:

<%# !String.IsNullOrEmpty(Eval(“address2”).ToString()) ? “<p>”+Eval(“address2”)+“</p>” : “” %>

I hope that is wat your trying to do. You will notice that i shortend
DataBinder.Eval(Container.DataItem, “address2”)
to
Eval(“address2”)

Coz i did not feel like typing all that out. lol. And this is going to use reflection either way, so i prefer the shorthand.

Another way of doing it would be to make the last <p> tag empty and make it runat server and watever ID you want. Then on the ItemDataBound event, check if address2 exists. If so, use the InnerHtml property and if not set Visible to false, which will stop if from rendering the output.

Thanks for your help that worked

<%# !String.IsNullOrEmpty(Eval(“address2”).ToString()) ? Eval(“address2”)+“<br />”:“”%>

I even cleaned up my code to use the shorthand

thanks again
:slight_smile: