在网页中,加载后,我用两个表填充数据集,其中包含这些表之间的关系,然后将数据加载到具有嵌套转发器的转发器中。用户单击按钮后也会发生这种情况。数据从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>
答案 0 :(得分:11)
DataItem
总是null
是真的吗?因为在访问DataItem
属性之前,您可能只需要检查ItemType属性:
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var data = e.Item.DataItem;
// ....
}
答案 1 :(得分:1)
您需要检查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();
}