views:

77

answers:

4
<% if (condition) { %>
<%= variable %>
<% } %>

or

<% if (condition) { 
Response.write(variable);
} %>
+1  A: 

The one that you consistently use though the rest of your codebase.

Robert
+2  A: 

I try to avoid both approaches you listed. But use a small wrapper method instead.

protected string DisplayVariable()
{
  // conditionals, etc. go in here
   ....
}

Then call...

<%= DisplayVariable() %>
kervin
A: 

Some other alternatives you could have considered would be ...

<%: condition ? variable : "" %>

or create an Extension method on HtmlHelper that takes a condition and a string.

<%=Html.OptionalMessage(condition,variable) %>
Hightechrider
A: 

Of your two supplied options, the second would be best because it is less to edit and easier to read.

However, I would urge you to also consider ideas from the other answers here; put as much code as possible into methods so you can have a really short call in the web page. This makes your web page logic much easier to read, especially for larger logic blocks and when there are more such blocks in one page.

Thor