变化中继李项类,如果第一个或最后一个第一个、李项类

由网友(失念 pain゜)分享简介:即时通讯使用中继器来创建动态UL李名单Im using repeater to create dynamic ul li list是否有可能控制类项目是第一个或最后?Is it possible to control class whether item is first or last ?某事,如:clas...

即时通讯使用中继器来创建动态UL李名单

Im using repeater to create dynamic ul li list

是否有可能控制类项目是第一个或最后?

Is it possible to control class whether item is first or last ?

某事,如:

class="<%# if(Container.ItemIndex == 0)
   {
        class = ... 
   }
   ) %>"

顺便说一句这是什么真正的意思:&LT;%#在asp.net

by the way what does it really mean: <%# in asp.net

什么之间&所述的差;%#和&lt;%=?

what is the difference between <%# and <%= ?

感谢帮助

推荐答案

这是很容易判断该项目是否是第一或没有( Container.ItemIndex == 0 ),但要确定元素是否为最后还是没有,你必须使用,将正确的数据绑定初始化的自定义属性:

It is quite easy to determine whether the item is first or not (Container.ItemIndex == 0), but to determine whether the element is last or not you have to use a custom property which will be initialized right with data binding:

protected int ItemCount { get; set; }

下面是一个转发器的例子:

Here is a repeater example:

<asp:Repeater runat="server" ID="repeater">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li class="<%# GetItemClass(Container.ItemIndex) %>">
            <%# Container.DataItem %>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

下面是数据绑定的一个例子:

here is an example of data binding:

public override void DataBind()
{
    var data = new string[] { "first", "second", "third" };
    this.ItemCount = data.Length;

    repeater.DataSource = data;
    repeater.DataBind();
}

和最后一个辅助方法:

protected string GetItemClass(int itemIndex)
{
    if (itemIndex == 0)
        return "first";
    else if (itemIndex == this.ItemCount - 1)
        return "last";
    else
        return "other";
}

这将产生:

<ul>
    <li class="first">
        first
    </li>
    <li class="other">
        second
    </li>
    <li class="last">
        third
    </li>
</ul>
阅读全文

相关推荐

最新文章