0

I have a datalist that I want to repeat items and use the auto(row alternating) color for an application I am working on. I am trying to add a header template above the item template like so.

<table>
<asp:DataList ID="DataList1" runat="server" CellPadding="4" DataKeyField="num" DataSourceID="SqlDataSource1" ForeColor="#333333" OnItemDataBound="DataList1_ItemDataBound">
        <AlternatingItemStyle BackColor="White" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <ItemStyle BackColor="#EFF3FB" />
        <HeaderTemplate>
        <tr>
            <th>Item Name</th>
            <th>Item Type</th>
            <th>Item Price</th>
            <th>End(s) In</th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>

            <tr>
            <td><a class="rolls2" href="ItemView.aspx?ItemName=<%# Eval("ItemName") %>&num=<%# Eval("num") %>"><%# Eval("ItemName") %></a></td>
            <td><%# Eval("ItemType")%></td>
            <td>$ <%# Eval("ItemPrice")%></td>
            <td><asp:Label ID="lblExp" Text='<%# Bind("CreateDate") %>' runat="server" /></td>
            </tr>

        </ItemTemplate>
        <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    </asp:DataList>
<table>

When I run this code I get a object not set to reference of the object and it points to lblExp. If I remove the header the project runs sucessfully. Is there another way to simply add a Header to my table?

Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • Why use an asp:Label and not a plain html label? – Brad M Jun 21 '13 at 15:27
  • So I can change the value of it later on. In my code I take the date time currently stored there and use it as an expiration date with some datetime math. – Skullomania Jun 21 '13 at 15:28
  • Can you post the code from your `DataList1_ItemDataBound` method? That's the most likely culprit. – Richard Deeming Jun 21 '13 at 16:17
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 21 '13 at 16:49

1 Answers1

2

It's difficult to diagnose this without seeing your code, but I would guess that your DataList1_ItemDataBound method is trying to find the lblExp control in the e.Item without checking the e.Item.ItemType first.

This event will be raised for every item in the list, including the header, and the header doesn't contain a control called lblExp.

You need to check the ItemType before processing the item:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
   switch (e.Item.ItemType)
   {
      case ListItemType.Item:
      case ListItemType.AlternatingItem:
      case ListItemType.SelectedItem:
      case ListItemType.EditItem:
      {
         Label lblExp = (Label)e.Item.FindControl("lblExp");
         ...
      }
   }
}
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151