Question

I have the following datalist:

<asp:DataList ID="DataList1" runat="server" DataKeyField="DocumentID" >
    <HeaderTemplate> 
   Name        </td>
   <td>       header 2       </td>
   <td>       header 3       </td>
   <td>       header 4       </td>
   <td>       viewed        </HeaderTemplate>

 <ItemTemplate>    
   <a href="#" id="doc_<%# Eval("DocumentID") %>" class="docPreview">
    <%# Eval("FileName")%>
   </a>
   </td>
   <td><asp:Label ID="Item2Label" runat="server" Text='<%# Eval("Item2")%>' /></td>
   <td><asp:Label ID="Item3Label" runat="server" Text='<%# Eval("Item3")%>' /></td>
   <td><asp:Label ID="Item4Label" runat="server" Text='<%# Eval("Item4")%>' /></td>
   <td>           
   <asp:CheckBox ID="isViewed" runat="server" Text='<%# Eval("DocumentID") %>' CssClass="hiddenText" />           
</ItemTemplate>
</asp:DataList>

This produces a valid HTML table.

However when I try and set AlternatingItemStyle-CssClass or use AlternatingItemStyle it only makes the change to the first td (of each alternating row) rather then the whole row. I've also tried using OnItemDataBound and setting the css class in the code behind, but it also only adds the class to the td rather then the tr.

Am I simply miss using the DataList, and is there a simple way to solve this?

Was it helpful?

Solution

DataList renders a table with a single TD for each item. The way you design your templates, you trick the DataList to show multiple columns. This results in valid HTML, but is not as the DataList is intended to be used. The control does not analyze the templates and check for further TDs that the styles should be applied to.
I'd suggest to use another control that is designed to show multiple controls:

  • DataGrid or GridView. A good comparison of the controls is available here.
  • If you need more control over the HTML that is rendered, use a Repeater.

All of the controls support different styles for alternating items (Repeater through a separate template for the alternating items which might result in some duplicated markup).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top