In this article we will try to understand how to create a nested ListView in ASP.Net 3.5.
Step 1
Create an Address class as:
public class Address
{
public string Country
{
get;
set;
}
public string State
{
get;
set;
}
public string City
{
get;
set;
}
}
Step 2
Create a Customer Class as:
public class Customer
{
public string CustomerName
{
get;
set;
}
public string Email
{
get;
set;
}
public string Website
{
get;
set;
}
public List<Address> Address
{
get;
set;
}
}
Step 3
Create a Customer.aspx as:
<asp:ListView runat="server" ItemPlaceholderID="placeHolderCustomer" ID="LstCustomers">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="placeHolderCustomer" />
</div>
</LayoutTemplate>
<ItemTemplate>
Customer Name:<asp:Label ID="Label1" Text='<%# Eval("CustomerName") %>' runat="server" /><br />
Email:<asp:Label ID="Label2" Text='<%# Eval("Email") %>' runat="server" /><br />
Website:<asp:Label ID="Label3" Text='<%# Eval("Website") %>' runat="server" />
<asp:ListView ID="ListView1" runat="server" DataSource='<%# Eval("Address") %>' ItemPlaceholderID="addressPlaceHolder">
<LayoutTemplate>
<div style="padding-left: 30px;">
<table border="1" cellpadding="0" cellspacing="0">
<asp:PlaceHolder runat="server" ID="addressPlaceHolder" />
</table>
</div>
<hr />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label4" Text='<%# Eval("Country") %>' runat="server" />
</td>
<td>
<asp:Label ID="Label3" Text='<%# Eval("State") %>' runat="server" />
</td>
<td>
<asp:Label ID="Label5" Text='<%# Eval("City") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
Step 4
Add the following code in Page Load of Customer.aspx:
List<Customer> objCustomers = new List<Customer>()
{
new Customer()
{
CustomerName="Customer 1",
Email="[email protected]",
Website="www.Customer1.com",
Address=new List<Address>()
{
new Address()
{
Country="Country1",
State="State1",
City="City1",
},
new Address()
{
Country="Country1",
State="State1",
City="City2",
},
new Address()
{
Country="Country1",
State="State2",
City="City3",
}
}
},
new Customer()
{
CustomerName="Customer 2",
Email="[email protected]",
Website="www.Customer2.com",
Address=new List<Address>()
{
new Address()
{
Country="Country2",
State="State2",
City="City4",
},
new Address()
{
Country="Country2",
State="State2",
City="City5",
},
new Address()
Country="Country2",
State="State2",
City="City6",
}
}
},
new Customer()
{
CustomerName="Customer 3",
Email="[email protected]",
Website="www.Customer3.com",
Address=new List<Address>()
{
new Address()
{
Country="Country3",
State="State4",
City="City7",
},
new Address()
{
Country="Country3",
State="State5",
City="City8",
},
new Address()
{
Country="Country3",
State="State6",
City="City9",
}
}
}
};
LstCustomers.DataSource = objCustomers;
LstCustomers.DataBind();