C# If 语句在中继器 ItemTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17168851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:44:52  来源:igfitidea点击:

If statement in repeaters ItemTemplate

c#asp.netrepeateritemtemplate

提问by Vivendi

I'm using an ASP.NET Repeaterto display the contents of a <table>. It looks something like this:

我正在使用 ASP.NETRepeater来显示<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>

It works fine, but i'd like to have an if()statement inside the ItemTemplateso i can conditionally determine if i want to print out a <tr>tag.

它工作正常,但我想if()在里面有一个语句,ItemTemplate这样我就可以有条件地确定是否要打印<tr>标签。

So i'd like to have something like this:

所以我想要这样的东西:

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

Is there some way i can achieve this?

有什么方法可以实现这一目标吗?

PS. The CurrentItemCountis just made up. I also need a way to get the current item count inside that if()statement. But i only seem to be able to get it from <%# Container.ItemIndex; %>, which can't be used with an if()statement?

附注。在CurrentItemCount刚刚组成。我还需要一种方法来获取该if()语句中的当前项目数。但是我似乎只能从 中获取它<%# Container.ItemIndex; %>,不能与if()语句一起使用?

采纳答案by Tim Schmelter

I would use codebehind:

我会使用代码隐藏:

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

回答by Claudio Redi

If you're trying yo make a 2 columns table this could do the trick

如果您正在尝试制作 2 列表,这可以解决问题

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

Changed a couple of things: id="itemRow"for all rows would cause repeated ids what is not allowed.

改变了几件事:id="itemRow"对于所有行会导致重复的 ids 是不允许的。

Removed runat="server"since doesn't make sense on this context.

删除,runat="server"因为在此上下文中没有意义。

回答by Amc Amsterdam - Netherlands

I have 2 examples, for the examples i will bind the repeater to a array of strings (demonstration purposes only)

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

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

Example 1: Create a methode in de codebehind casting the bound elements back en evaluate what ever value you'd like.

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

Create Methode in CodeBehind (example 1):

在 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 ""; 
    }
}

In the .aspx file (example 1):

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

Example 2: You could use a direct cast in the .aspx file

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

DirectCast example (no code behind):

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>

I hope this is what you're looking for. Regards.

我希望这就是你要找的。问候。

回答by kduenke

If you're wanting to do something on every other item, use the alternating item template. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx

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

回答by maets

Another way of doing this (if performance is not a problem):

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

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