views:

59

answers:

4

Hey All,

<% if (isTrue){ %>
      <div id="myResults" style="display: none;">
<% } else { %>
      <div id="myResults" >    
<% } %>

Is there another way to write the preceding? The only difference is displaying it or not based on a condition.

Thanks, rodchar

+6  A: 

Here's another way, but I am not sure I like it better than the original ;)

<div id="myResults"<%= isTrue ? " style=\"display: none;\"" : "" %>>

If you want something that is both concise and readable, consider switching to the NHaml view engine:

#myResults{ style = isTrue ? "display: none" : null }
Jørn Schou-Rode
A: 
<div id="myResults"<%= isTrue ? ' style="display: none;"' : '' %>>

Perhaps there's an even shorter notation.

Rygu
+1  A: 

Even though Jörns example will work, you might want to consider putting this in an extension method:

<%= Html.Results(isTrue) %>

And in a class in your library:

public static class MyHtmlExtensions

    public static string Results(this HtmlHelper helper, bool hidden)
    {
        return String.Format("<div id=""myResults"" {0}>",
                             hidden = ? "style=""display: none;""" : "");
    }
}
Tomas Lycken
+1  A: 

since your using styles, you could get it out of the body of the html altogether...

<style type="text/css">
div#myResults { display: <$= isTrue ? "none" : "block" %>; }
</style>
...
<div id=myResults>
David