变化中继李项类,如果第一个或最后一个 [英] change repeater li item class if first or last

查看:143
本文介绍了变化中继李项类,如果第一个或最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用中继器来创建动态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>

这篇关于变化中继李项类,如果第一个或最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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