【问题标题】:If statement in repeaters ItemTemplate中继器 ItemTemplate 中的 If 语句
【发布时间】:2013-06-18 12:27:11
【问题描述】:

我正在使用 ASP.NET Repeater 来显示 <table> 的内容。它看起来像这样:

<table cellpadding="0" cellspacing="0">
    <asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound">
        <ItemTemplate>
            <tr id="itemRow" runat="server">
                <td>
                    Some data
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

它工作正常,但我想在ItemTemplate 中有一个if() 语句,这样我可以有条件地确定是否要打印出&lt;tr&gt; 标记。

所以我想要这样的东西:

<table cellpadding="0" cellspacing="0">
    <asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound">
        <ItemTemplate>

            <% if ( (CurrentItemCount % 2) == 0 ) { %?>
            <tr id="itemRow" runat="server">
            <% } %>
                <td>
                    Some data
                </td>
            <% if ( (CurrentItemCount % 2) == 0 ) { %?>
            </tr>
            <% } %>
        </ItemTemplate>
    </asp:Repeater>
</table>

有什么方法可以实现吗?

PS。 CurrentItemCount 刚刚编好。我还需要一种方法来获取 if() 语句中的当前项目数。但我似乎只能从&lt;%# Container.ItemIndex; %&gt; 获得它,它不能与if() 语句一起使用?

【问题讨论】:

  • 不能使用gridview显示表格数据有什么原因吗?
  • @Bartdude 是的,我正在调整现有代码,我真的不想重写很多功能。因此,如果我的代码能够以某种方式实现,那么我真的很想坚持下去。

标签: c# asp.net repeater itemtemplate


【解决方案1】:

另一种方法(如果性能不是问题):

<ItemTemplate>
  <!-- "If"  -->
  <asp:PlaceHolder runat="server" Visible="<%# MyCondition %>">
    <tr><td></td></tr>
  </asp:PlaceHolder>  
  <!-- "Else" -->
  <asp:PlaceHolder runat="server" Visible="<%# !MyCondition %>">
    <tr><td></td></tr>
  </asp:PlaceHolder>
</ItemTemplate>

【讨论】:

  • 嗯,当然很大程度上取决于您的情况。我的经验是,在正常用例中,您最终会在所有占位符中多次执行相同的指令,每个条件一个。隐藏占位符中的指令仍然会被执行,即使没有标记被发送到客户端。
  • 当我们将 Visible 设置为 false 时,标记不会发送给客户端。
  • 是的,但我认为在可见性条件不是静态的情况下(根据数据绑定确定),两个占位符中定义的所有逻辑都将在服务器上评估/执行。可能会导致性能下降,但不一定。
  • 我用这个没有性能问题。我的中继器只有 100 到 200 行。条件内容的完美解决方案。
【解决方案2】:

如果您尝试制作一个 2 列的表格,这可以解决问题

<%# Container.ItemIndex % 2 == 0 ? "<tr class='itemRow'>" : "" %>
    <td>
       Some data
    </td>
<%# Container.ItemIndex % 2 != 0 ? "</tr> : "" %>

更改了一些内容:所有行的id="itemRow" 会导致重复的 ID,这是不允许的。

删除了runat="server",因为在这种情况下没有意义。

【讨论】:

    【解决方案3】:

    我有 2 个示例,对于示例,我会将转发器绑定到字符串数组(仅用于演示目的)

    void BindCheckboxList()
    {
     checkboxList.DataSource = new string[] { "RowA", "RowB", "RowC", "RowD", "RowE", "RowF", "RowG" };
     checkboxList.DataBind();
    }
    

    示例 1:在 de codebehind 中创建一个方法,将绑定的元素投射回去,然后评估您想要的任何值。

    在 CodeBehind 中创建 Methode(示例 1):

    protected string StringDataEndsWith(object dataElement, string endsWith, string  returnValue)
    {
    // for now an object of the type string, can be anything.
    string elem = dataElement as string;
        if (elem.EndsWith(endsWith))
        {
         return returnValue; 
        }
         else
        {
         return ""; 
        }
    }
    

    在 .aspx 文件中(示例 1):

    <asp:Repeater ID="checkboxList" runat="server">
    <HeaderTemplate> 
        <table style="padding:0px;margin:0px;">
    </HeaderTemplate> 
    <ItemTemplate>
        <%# StringDataEndsWith(Container.DataItem,"A","<tr id=\"itemRow\" runat=\"server\">")  %>
        <td>
            <%# Container.DataItem  %>
        </td>
        <%# StringDataEndsWith(Container.DataItem,"G","</tr>")  %>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    </asp:Repeater>
    

    示例 2:您可以在 .aspx 文件中使用直接强制转换

    DirectCast 示例(后面没有代码):

    <asp:Repeater ID="checkboxList" runat="server">
    <HeaderTemplate> 
        <table style="padding:0px;margin:0px;">
    </HeaderTemplate> 
    <ItemTemplate>
        <%# Convert.ToString(Container.DataItem).EndsWith("A") ? "<tr id=\"itemRow\" runat=\"server\">" : ""  %>
        <td>
            <%# Container.DataItem  %>
        </td>
        <%# Convert.ToString(Container.DataItem).EndsWith("G") ? "</tr>" : ""  %>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    </asp:Repeater>
    

    我希望这是您正在寻找的。问候。

    【讨论】:

      【解决方案4】:

      如果您想对其他所有项目执行操作,请使用交替项目模板。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx

      【讨论】:

        【解决方案5】:

        我会使用代码隐藏:

        protected void OnCheckboxListItemBound(Object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlTableRow itemRow = (HtmlTableRow) e.Item.FindControl("itemRow");
                itemRow.Visible = e.Item.ItemIndex % 2 == 0;
            }
        }
        

        【讨论】:

        • 因为我需要保留行的ID 属性,所以这对我来说是最好的解决方案。
        • 此答案未显示如何在 ItemTemplate 中使用 if 语句。它应该只是一个评论。
        • @edward:但如果您想使用 if 语句并使用项目模板,它显示了最好的方法。在一般故障安全和编译时间安全方面最好。它也是更具可读性和可维护性的方式。实际上,即使是 OP 也更喜欢这个。如果我仍然想编写经典的 ASP 或 PHP,我不会使用 ASP.NET。除此之外,我不能把这一切都放在评论中。
        • 可以说它的可读性和可维护性较差。 ASP.NET 的标准方法是在标记中包含 UI 逻辑,在 C# 文件中包含业务逻辑。将 UI 逻辑放入 C# 文件几乎与 ASP 和 PHP 将业务逻辑代码放入标记中一样糟糕。这是我发现实际上正确执行此操作的示例之一:stackoverflow.com/a/264445/556649
        • aspx 仅用于控件和样式表(布局)。代码或逻辑属于代码隐藏。恕我直言,内联代码仅用于测试或演示目的,除了 Eval/Bind 或 javascript。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多