1

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.

4

1 回答 1

0

从. OnItemDataBound="dlOutNotice_ItemDataBound"_ dlOutNotice DataListOnDataBinding="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;
}
于 2013-05-23T16:33:31.870 回答