该问题的解决方法是使用数据绑定的方法。具体步骤如下:
将外部Listview和内部Listview绑定到数据源
在外部Listview的ItemDataBound事件中,找到内部Listview并绑定数据源
以下是示例代码:
VB.NET:
Protected Sub ListView1_OnItemDataBound(sender As Object, e As ListViewItemEventArgs) Dim innerListView As ListView = CType(e.Item.FindControl("InnerListView"),ListView)
If (innerListView IsNot Nothing) Then
Dim dataItem As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim innerData As DataTable = GetInnerData(dataItem("ID"))
innerListView.DataSource = innerData
innerListView.DataBind()
End If
End Sub
C#:
protected void ListView1_OnItemDataBound(object sender, ListViewItemEventArgs e) { ListView innerListView = e.Item.FindControl("InnerListView") as ListView;
if(innerListView!=null)
{
DataRowView dataItem = e.Item.DataItem as DataRowView;
DataTable innerData = GetInnerData(dataItem["ID"]);
innerListView.DataSource = innerData;
innerListView.DataBind();
}
}