ASP.Net repeater item.DataItem is null

2019-06-27 22:52发布

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>

2条回答
淡お忘
2楼-- · 2019-06-27 23:17

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;
    // ....
}
查看更多
做自己的国王
3楼-- · 2019-06-27 23:22

You need to check the ItemType.

So in your ItemDataBound Event.

switch (e.Item.ItemType) {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                 RepeaterItem item = e.Item; 

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

}
查看更多
登录 后发表回答