Question

I have one Repeater

<asp:Repeater runat="server" ID="rptID" OnItemDataBound="repID_ItemDataBound">
         <ItemTemplate>
         <a href='example.com/somepage.aspx' id="myLink">
                 <%# Eval("MyVal")%> 
         </a>
        </ItemTemplate>
</asp:Repeater>

In Code Behind I need to add one css class for this <a> tag when in repeater is one Item

protected void repID_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
      if (e.Item.ItemType == ListItemType.Item || 
          e.Item.ItemType == ListItemType.AlternatingItem)
      {
          //how to set this class only then count of items is equal with 1
          ((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
                                                     .Add("class", "Count1");
      }
  }
Était-ce utile?

La solution

This will give you the count of your datasource items:

if (((IEnumerable)rptID.DataSource).Cast<object>().Count() == 1)
{
    ((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
                                                 .Add("class", "Count1");
}

Counting the IEnumerable was borrowed from this thread: Calculating Count for IEnumerable (Non Generic)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top