ASP.Net 转发器 item.DataItem 为 null

发布于 2024-10-10 23:44:27 字数 4217 浏览 2 评论 0原文

在网页中,加载时,我用两个表填充数据集以及这些表之间的关系,然后将数据加载到具有嵌套转发器的转发器中。用户单击按钮后也可能发生这种情况。数据从 SQL 数据库加载,转发器数据源设置为回发后的数据集。但是,当 ItemDataBound 发生时,Item.Dataitem 始终为 null。

为什么会出现这种情况呢?

这是数据绑定代码:

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

这是 ItemDataBound 代码:

        RepeaterItem item = e.Item; 

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

下面是我的 HTML 重复器代码

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-10-17 23:44:27

DataItem 始终为 null 是真的吗?因为也许您只需要检查 访问 DataItem 属性之前的 ItemType 属性:

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

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;
    // ....
}
娇妻 2024-10-17 23:44:27

您需要检查 ItemType。

所以在你的 ItemDataBound 事件中。

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();

}

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();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文