I have a datalist "dlBldgNames" in another datalist "dlOutNotice". The relationship is each one OutNotice can have many Buildings. The code is like below:
<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>
Code behind is like this:
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();
}
}
But the inside datalist is all same in each item of outside datalist . And it looks they all shows last record of eventid.