如何在转发器中执行if语句 [英] How can i do an if statement inside a repeater

查看:153
本文介绍了如何在转发器中执行if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< asp:Repeater> 让我发疯...

我需要做

<ItemTemplate>
    <% if (Container.DataItem("property") == "test") {%>
        I show this HTML
    <% } else { %>
        I show this other HTML
    <% } %>
</ItemTemplate>

但我不能为我的生活找到任何方法来实现这一目标。三元不是很好,因为HTML的数量非常大,通过DataBind事件设置标签也不是很好,因为我必须在代码隐藏中拥有大块的HTML。

But I can't for the life of me find any way to make that happen. Ternary isnt any good, because the amount of HTML is quite large, setting labels via a DataBind event is not very good either, as I'd have to have large blocks of HTML in the code-behind.

肯定有办法做到这一点....

Surely there's a way to do this....

推荐答案

你可以尝试创建一种在ViewModel类中,对代码隐藏做出决定,然后对转发器感到满意,只需显示正在给出的数据。

You could try creating a kind of ViewModel class, do the decision on your code-behind, and then be happy with your repeater, simply displaying the data that it is being given.

这是一种方式将逻辑与UI分开。然后,你可以拥有一个简单显示数据的哑巴UI,而无需决定显示什么/如何显示。

This is a way to separate logic from UI. You can then have a dumb-UI that simply displays data, without having to decide on what/how to show.

这篇关于如何在转发器中执行if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆