ASP.Net 转发器 item.DataItem 为 null
在网页中,加载时,我用两个表填充数据集以及这些表之间的关系,然后将数据加载到具有嵌套转发器的转发器中。用户单击按钮后也可能发生这种情况。数据从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DataItem
始终为null
是真的吗?因为也许您只需要检查 访问DataItem
属性之前的 ItemType 属性:Is is true that the
DataItem
is ALWAYSnull
? Because maybe you just need to check for the ItemType property before accessing theDataItem
property:您需要检查 ItemType。
所以在你的 ItemDataBound 事件中。
You need to check the ItemType.
So in your ItemDataBound Event.