ASP.Net repeater item.DataItem is null

mattgcon picture mattgcon · Jan 10, 2011 · Viewed 20k times · Source

Within a webpage, upon loading, I fill a dataset with two table with a relation between those tables and then load the data into a repeater with a nested repeater. This can also occur after the user clicks on a button. The data gets loaded from a SQL database and the repeater datasource is set to the dataset after a postback. However, when ItemDataBound occurs the Item.Dataitem is always null.

Why would this occur?

Here is the databind code:

        this.rptCustomSpaList.DataSource = ds;
        this.rptCustomSpaList.DataBind();

Here is the ItemDataBound code:

        RepeaterItem item = e.Item; 

        Repeater ChildRepeater = (Repeater)item.FindControl("rptCustomSpaItem");
        DataRowView dv = e.Item.DataItem as DataRowView;
        ChildRepeater.DataSource = dv.CreateChildView("sparelation");
        ChildRepeater.DataBind();

below is my HTML repeater code

<asp:Repeater ID="rptCustomSpaList" runat="server" 
 onitemdatabound="rptCustomSpaList_ItemDataBound">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
        <table>
            <tr>
                <td><asp:Label ID="Label3" runat="server" Text="Spa Series:"></asp:Label></td>
                <td><asp:Label ID="Label4" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SPASERIESVALUE") %>'></asp:Label></td>
            </tr>
            <tr>
                <td><asp:Label ID="Label5" runat="server" Text="Spa Model:"></asp:Label></td>
                <td><asp:Label ID="Label6" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SPAMODELVALUE") %>'></asp:Label></td>
            </tr>
            <tr>
                <td><asp:Label ID="Label9" runat="server" Text="Acrylic Color:"></asp:Label></td>
               <td><asp:Label ID="Label10" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ACRYLICCOLORVALUE") %>'></asp:Label></td>
            </tr>
            <tr>
  <td>
   <asp:Label ID="Label11" runat="server" Text="Cabinet Color:"></asp:Label>
  </td>
  <td>
   <asp:Label ID="Label12" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CABPANCOLORVALUE") %>'></asp:Label>
  </td>
            </tr>
            <tr>
  <td>
   <asp:Label ID="Label17" runat="server" Text="Cabinet Type:"></asp:Label>
  </td>
  <td>
   <asp:Label ID="Label18" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CABINETVALUE") %>'></asp:Label>
  </td>
  </tr>
  <tr>
  <td>
   <asp:Label ID="Label13" runat="server" Text="Cover Color:"></asp:Label>
  </td>
  <td>
   <asp:Label ID="Label14" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "COVERCOLORVALUE") %>'></asp:Label>
  </td>
  </tr>
  </table>
  <asp:Label ID="Label15" runat="server" Text="Options:"></asp:Label>
  <asp:Repeater ID="rptCustomSpaItem" runat="server">
   <HeaderTemplate>
    <table>
   </HeaderTemplate>
   <ItemTemplate>
    <tr>
    <td>
    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "PROPERTY") %>'></asp:Label>
    </td>
    <td>
    <asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "VALUE") %>'></asp:Label>
    </td>
    </tr>
   </ItemTemplate>
   <FooterTemplate>
    </table>
   </FooterTemplate>
  </asp:Repeater>
  <table>
  <tr>
  <td style="padding-top:15px;padding-bottom:30px;">
   <asp:Label ID="Label7" runat="server" Text="Configured Price:"></asp:Label>
  </td>
  <td style="padding-top:15px;padding-bottom:30px;">
   <asp:Label ID="Label8" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SPAVALUEVALUE") %>'></asp:Label>
  </td>
  </tr>
  </table>
  <asp:Label ID="Label16" runat="server" Text="------"></asp:Label>
 </ItemTemplate>
 <FooterTemplate></FooterTemplate>
</asp:Repeater>

Answer

volpav picture volpav · Jan 10, 2011

Is is true that the DataItem is ALWAYS null? Because maybe you just need to check for the ItemType property before accessing the DataItem property:

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
{
    var data = e.Item.DataItem;
    // ....
}