我在另一个数据列表“dlOutNotice”中有一个数据列表“dlBldgNames”。这个关系是每个OutNotice可以有很多建筑物。代码如下:
<asp:DataList ID="dlOutNotice" runat="server" DataSourceID="dsOutNotice" onitemdatabound="dlOutNotice_ItemDataBound" >
<ItemTemplate>
Description: <asp:Label ID="OutLogIDLabel" runat="server" Text='<%# Eval("OutDesp") + " [" + Eval("Comment") + "]."%>' />
Building(s) Affected: <asp:DataList ID="dlBldgNames" runat="server" DataSourceID="dsBldgNames" DataKeyField="BldgID" >
<ItemStyle HorizontalAlign="Center" /><ItemTemplate><asp:LinkButton ID="lbBldg" runat="server" Text='<%# Eval("CommonName") %>' /></ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="dsBldgNames" runat="server" SelectMethod="GetBldgsName" TypeName="test.DAL.Repository">
<SelectParameters>
<asp:Parameter Name="eventid" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="dsOutNotice" runat="server" SelectMethod="GetOutNotice" TypeName="test.DAL.Repository">
<SelectParameters>
<asp:ControlParameter ControlID="txtDateS" Name="date" PropertyName="Text" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>
背后的代码是这样的:
protected void dlOutNotice_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlBldgNames = e.Item.FindControl("dlBldgNames") as DataList;
string eventid = dlOutNotice.DataKeys[e.Item.ItemIndex].ToString();
dsBldgNames.SelectParameters["eventid"].DefaultValue = eventid;
dlBldgNames.DataBind();
}
}
但是内部数据列表在外部数据列表的每个项目中都全部相同。它看起来都显示了最后的记录。
答案 0 :(得分:0)
从OnItemDataBound="dlOutNotice_ItemDataBound"
dlOutNotice
中删除DataList
属性。将OnDataBinding="dlBldgNames_DataBinding"
属性添加到dlBldgNames
DataList
。这是相应的方法:
protected void dlBldgNames_DataBinding(object sender, EventArgs e)
{
var current = sender as DataList;
var item = current.Parent as DataListItem;
var parent = item.Parent as DataList;
var eventId = parent.DataKeys[item.ItemIndex].ToString();
dsBldgNames.SelectParameters["eventid"].DefaultValue = eventId;
}