Change repeater li item class if first or last

前端 未结 10 2125
时光说笑
时光说笑 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:35

    I typically use something like the following:

    <asp:Repeater ID="rptItems" runat="server" ViewStateMode="Disabled">
      <ItemTemplate>
        <li<%# Container.ItemIndex == ((IList)((Repeater)Container.Parent).DataSource).Count-1 ? " class='last'" : ""%>>
    ...
        </li>
      </ItemTemplate>
    </asp:Repeater>
    
    0 讨论(0)
  • 2020-12-05 07:35

    As for your question on the difference between <%= and <%#, please see the following links:

    Code Render Blocks:

    http://msdn.microsoft.com/en-us/library/k6xeyd4z(v=vs.71).aspx

    Data Binding Expression Syntax:

    http://msdn.microsoft.com/en-us/library/bda9bbfx(v=vs.71).aspx

    0 讨论(0)
  • 2020-12-05 07:37

    Try somwthing like this, if you are not using jQuery

     <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:Label ID="Label1" CssClass='<%# Container.ItemIndex == 0 ? "first" : "notFirst" %>' runat="server" Text="Label"></asp:Label>
        </ItemTemplate>
        </asp:Repeater>
    
    0 讨论(0)
  • 2020-12-05 07:37

    If your are using controls within your ItemTemplate you can do something like the following, add OnItemDataBound event.

    <asp:Repeater ID="RptId" runat="server" OnItemDataBound="OnItemDataBound">
      <ItemTemplate>
        <asp:Panel runat="server" ID="PnlCtrlItem">
          <%#Eval("Content") %>
        </asp:Panel>
      </ItemTemplate>
    </asp:Repeater>
    
    protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex == 0)
          ((Panel) e.Item.FindControl("PnlCtrlItem")).CssClass = "first";
        //or the following to have your control indexed
        ((Panel) e.Item.FindControl("PnlCtrlItem")).CssClass = string.Format("item-{0}",e.Item.ItemIndex);
    }
    
    0 讨论(0)
提交回复
热议问题