ListView 中的 if 语句使用 Eval() 或 DataBinder.Eval()?
我在 .aspx 页面上有一个列表视图控件。在此列表视图中,我想检查来自数据库的“类型”属性。这是示例代码:
<ItemTemplate>
<%# if(Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 0){ %>
<tr class="item">
<td>
<%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %>
</td>
<td style="text-align: center;">
<%# Eval("SkillName") %>
</td>
</tr>
<%# } else if (Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 1) {%>
<tr class="item">
<td colspan="2">
<strong><%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %></strong>
</td>
</tr>
<% } %>
</ItemTemplate>
作为最后的手段,我尝试使用 DataBinder.Eval() 但出现异常“预期的类、委托、枚举、接口或结构”。我可能做错了什么?在代码隐藏中编写函数对我来说不是一个选择。有办法实现这一点吗?
I have a listview control on an .aspx page. Inside this list view i want to check "Type" property which comes from database. here is the example code :
<ItemTemplate>
<%# if(Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 0){ %>
<tr class="item">
<td>
<%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %>
</td>
<td style="text-align: center;">
<%# Eval("SkillName") %>
</td>
</tr>
<%# } else if (Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Type")) == 1) {%>
<tr class="item">
<td colspan="2">
<strong><%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %></strong>
</td>
</tr>
<% } %>
</ItemTemplate>
As a last resort i tried to user DataBinder.Eval() but i get the exception "Expected class, delegate, enum, interface, or struct". What can i be doing wrong? Writing a function in code-behind isn't an option for me. Is there a way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是完整的代码,花哨且简短。
Here is the full code, made fancy and short.
未经测试,因为我目前没有可用的 Visual Studio,但自从 HtmlTableRow 有一个
Visible
属性,以下应该可以工作:Untested, as I don't have Visual Studio available at the moment, but since HtmlTableRow has a
Visible
property, the following should work:是的,你将不得不做一些客户端脚本...我建议jquery..
你基本上会遍历jquery中的所有行,并且根据行中的数据,你将能够更改行的innerhtml基于“.item”选择器的对象来确定它是否应该采用一种格式或另一种格式。
yes you will have to do some client side scripting though... I would suggest jquery..
you would basically loop through all of the rows in jquery and based on the data in the row you would be able to change the innerhtml of the row object based on the ".item" selector to determine whether it should be in one format or the other.