Reputation: 13
I am creating an asp.net page which will allow users to search across multiple databases.
Where a match is found I would like the database name to return in the main ListView. I then want to display, under their corresponding database names, any companies matching the search criteria in a nested ListView.
e.g. Search: The Company
Results
Database 1 Name
The Company 123
The Company Abc
Database 2 Name
The Company Xyz
Database 3 Name
The Company Test
How would I go about referencing and populating the nested ListView?
Upvotes: 1
Views: 2160
Reputation: 62260
You can use Parent ListView OnItemDataBound event, and bind Child ListView.
<asp:ListView ID="DatabaseListView" runat="server"
OnItemDataBound="DatabaseListView_ItemDataBound">
<ItemTemplate>
<h1><%# Eval("Name") %></h1>
<asp:ListView ID="CompanyListView" runat="server">
<ItemTemplate>
<p><%# Eval("Name") %></p>
</ItemTemplate>
</asp:ListView>
<hr />
</ItemTemplate>
</asp:ListView>
public class Database
{
public string Name { get; set; }
public IList<Company> Companies;
public Database()
{
Companies = new List<Company>();
}
}
public class Company
{
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var databases = new List<Database>
{
new Database
{
Name = "Database 1 Name",
Companies = new List<Company>
{
new Company {Name = "123"},
new Company {Name = "Abc"}
}
},
new Database
{
Name = "Database 2 Name",
Companies = new List<Company> {new Company {Name = "Xyz"}}
},
new Database
{
Name = "Database 3 Name",
Companies = new List<Company> {new Company {Name = "Test"}}
},
};
DatabaseListView.DataSource = databases;
DatabaseListView.DataBind();
}
}
protected void DatabaseListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var database = e.Item.DataItem as Database;
var companyListView = e.Item.FindControl("CompanyListView") as ListView;
companyListView.DataSource = database.Companies;
companyListView.DataBind();
}
}
Upvotes: 3