Mon*_*ers 6 c# if-statement repeater
<asp:Repeater> 让我发疯
我需要去做
<ItemTemplate>
<% if (Container.DataItem("property") == "test") {%>
I show this HTML
<% } else { %>
I show this other HTML
<% } %>
</ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
但我无法为我的生活找到任何方法来实现这一目标.三元不是很好,因为HTML的数量非常大,通过DataBind事件设置标签也不是很好,因为我必须在代码隐藏中拥有大块的HTML.
当然有办法做到这一点....
Gav*_*n H 20
您可以使用服务器端可见性:
<ItemTemplate>
<div runat="server" visible='<% (Container.DataItem("property") == "test") %>'>
I show this HTML
</div>
<div runat="server" visible='<% (Container.DataItem("property") != "test") %>'>
I show this other HTML
</div>
</ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
您可以尝试创建一种ViewModel类,对代码隐藏做出决定,然后对转发器感到满意,只需显示正在给出的数据.
这是一种将逻辑与UI分离的方法.然后,您可以使用一个简单的UI来显示数据,而无需决定显示什么/如何显示.