ListView 中的 if 语句使用 Eval() 或 DataBinder.Eval()?

发布于 2024-10-02 11:24:09 字数 1042 浏览 0 评论 0原文

我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扮仙女 2024-10-09 11:24:09

这是完整的代码,花哨且简短。

 <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>

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>
留一抹残留的笑 2024-10-09 11:24:09

未经测试,因为我目前没有可用的 Visual Studio,但自从 HtmlTableRow 有一个 Visible 属性,以下应该可以工作:

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

Untested, as I don't have Visual Studio available at the moment, but since HtmlTableRow has a Visible property, the following should work:

<tr class="item" runat="server" Visible='<%# Convert.ToInt32(Eval("Type")) == 0 %>'>
    ...
</tr>
往事随风而去 2024-10-09 11:24:09

是的,你将不得不做一些客户端脚本...我建议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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文