根据条件隐藏 ascx 标记

Hide ascx markup based on condition

我在 ascx 中有一些小标记,例如这个简单的 href,例如:

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

(如果代码隐藏文件中的变量为空,我想隐藏。) 显然上面的代码是一次尝试,需要调整才能在 ascx 中工作。

执行此操作的好方法是什么?例如,有没有办法将标记放在 ascx 中的某种占位符中并使其在条件下可见?语法提示表示赞赏。非常感谢。

与您使用 <% %> 的方式类似,只是不添加 =,您的代码将是:

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

您还可以阅读Scott Guthrie block for that syntax