aspx 中的条件标记
我有一个列表视图。如果我想将 html 标记基于数据绑定项的条件,那么最好的方法是什么?
我的意思是,除了 <% %> 之外,还有其他方法吗? if/else 直接在标记中阻塞?
我知道这样做的一种非常丑陋的方法是将 html 标记放入数据库字段中,然后让 Eval() 挤出正确的标记(我不这样做)。
我想避免将实际的 <% %> C# 也在代码中阻塞。
有什么好主意吗?
编辑:我正在考虑将 3 个不同的 asp:panels 设置为visible:false,然后在列表视图的数据绑定事件中,根据条件设置适当的面板visible:true - 有人对这个想法有任何设计意见吗?
I have a ListView. If I want to base the html markup on a condition in respects to the databound item, what would be the best way to do that?
What I mean is, is there any other way then putting <% %> if/else blocks directly in the markup?
I'm aware that a really ugly way of doing it, is putting html markup in the database field, and just let the Eval() squeeze out the proper markup (I'm not doing that).
I would like to avoid putting actual <% %> C# blocks in the code as well.
Any good ideas?
EDIT: I'm thinking about making 3 different asp:panels set visible:false, and then in the databound event of the listview, set the appropriate panel visible:true based on the condition - anyone have any design-opinions about this idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已解决...
最终得到 3 个面板,然后检查 itemDataBound 事件中的条件...
不过,我仍然想了解此解决方案的设计含义。
Solved...
Ended up with the 3 panels, and then checking the condition in the itemDataBound event...
I'd still like opinion on the design implications of this solution though.
重写列表视图“单元格”之一的“渲染”事件之一应该可以解决问题。考虑到其他选择,编写自己的条件标记代码并不是那么糟糕。
但请尽可能多地使用 CSS 来构建页面的真实结构。
Overriding one of the 'render' event of one of the listview 'cells' should do the trick. Writing you own code for conditional markup isn't that bad considering the alternatives.
But please do use as much as possible CSS for the real makeup of the page.