Dear2 Dear2 Dear2 Dear2 - 7 months ago 37
HTML Question

Hide ascx markup based on condition

I have some small markup in an ascx, for example this simple a href, like:

if (siteUrl) {
<a href="<%= siteUrl %>"> <%= Message here %> </a>
}
// otherwise don't display Url and message


(that I want to hide if a variable in the code behind file is null.)
Obviously the code above is an attempt and needs adjusting to work in ascx.

What is a good way to do this? Is there a way for example to put the markup in some sort of placeholder in the ascx and make that visible on condition? Syntax tips appreciated. Thanks a bunch.

Answer

Similar way you use the <% %>, just not add the =, and your code will be as:

<% if (!string.IsNullOrEmpty(siteUrl)) {%>
    <a href="<%= siteUrl %>"> <%= MessageMe %> </a> 
<% }%>

You can also read Scott Guthrie block for that syntax