简体   繁体   English

如果带有Eval()或DataBinder.Eval()的ListView中的语句?

[英]If statement inside a ListView with Eval() or DataBinder.Eval()?

I have a listview control on an .aspx page. 我在.aspx页面上有一个listview控件。 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". 作为最后的手段我尝试使用DataBinder.Eval(),但我得到异常“预期的类,委托,枚举,接口或结构”。 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? 有没有办法实现这个目标?

Here is the full code, made fancy and short. 这是完整的代码,花哨而简短。

 <ItemTemplate>
         <tr class="item">
            <td colspan="<%# Eval(Container.DataItem,"Type")) == 0 ? 1:2%>">
                <strong><%# Convert.ToDateTime(Eval("WorkDate")).ToShortDateString() %></strong>
            </td>
             <td style="text-align: center;" visible="<%# Eval(Container.DataItem,"Type")) == 1>">
                <%# Eval("SkillName") %>
            </td>
        </tr>
 </ItemTemplate>

Untested, as I don't have Visual Studio available at the moment, but since HtmlTableRow has a Visible property, the following should work: 未经测试,因为我目前没有可用的Visual Studio,但由于HtmlTableRow具有Visible属性,因此以下内容应该有效:

<tr class="item" runat="server" Visible='<%# Convert.ToInt32(Eval("Type")) == 0 %>'>
    ...
</tr>

yes you will have to do some client side scripting though... I would suggest jquery.. 是的,你将不得不做一些客户端脚本虽然...我会建议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. 你基本上循环遍历jquery中的所有行,并根据行中的数据,你可以根据“.item”选择器更改行对象的innerhtml,以确定它是否应该采用一种格式或者其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM