1

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?

1 Answer 1

3

You can use Parent ListView OnItemDataBound event, and bind Child ListView.

enter image description here

<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>

Code Behind

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.