Change repeater li item class if first or last

前端 未结 10 2169
时光说笑
时光说笑 2020-12-05 07:11

I\'m using repeater to create dynamic ul li list

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

Something like:

class=\"<%         


        
10条回答
  •  臣服心动
    2020-12-05 07:28

    I wasn't able to use Alex's answer directly. Here are the modifications I made that worked for me. Using Alex's "repeater example" for the asp:Repeater tag, use this in the code behind:

    private string[] data = new string[] { "first", "second", "third" };
    protected int ItemCount { get; set; }
    
    private void Page_Load(object sender, EventArgs e)
    {
        // normally one would fetch the data here right before binding like this:
        // data = SomeService.SomeMethodToGetData();
        repeater.DataSource = data;
        repeater.DataBind();
    }
    
    public override void DataBind()
    {
        ItemCount = data.Count();
    }
    
    protected string GetItemClass(int itemIndex)
    {
        if (itemIndex == 0)
            return "first";
        else if (itemIndex == this.ItemCount - 1)
            return "last";
        else
            return "other";
    }
    

提交回复
热议问题