如果转发器的ItemTemplate声明 [英] If statement in repeaters ItemTemplate

查看:170
本文介绍了如果转发器的ItemTemplate声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ASP.NET 转发来显示&LT的内容;表> 。它看起来是这样的:

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

<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 这样的如果()语句我可以有条件地确定如果我想打印出&LT; TR方式&gt; 标签

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

所以,我想有这样的事情:

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。在 CurrentItemCount 刚刚组成。我还需要一种方式来获得的如果()语句中的当前项目数。但我只似乎能够从中获取它&LT;%#Container.ItemIndex; %GT; ,无法与如果()语句中使用

PS. The CurrentItemCount is 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?

推荐答案

我会用codebehind:

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

这篇关于如果转发器的ItemTemplate声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆