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();


Lee RayPosted Jan 23, 2014, 3:00 PM EST
so awesome!
Rajendar reddyPosted Jan 5, 2013, 8:04 PM EST
great article.....Thanks http://www.microsoft-csharp-tutorials.com/post/2012/12/23/Listview-Control-in-Net-ItemInsert-in-Listview-ItemTemplate-and-AlternatingItemTemplate-in-Listview
Vithal WadjeTOP 100Posted Dec 31, 2012, 10:52 PM EST
super work
Mustafa IqbalPosted Oct 31, 2012, 1:11 AM EDT
Thanks, your article is helpful for me, but one more thing confusing me, that how can I set image in listview from SQL datasource (Image is in binary format)?
Sandeep Singh ShekhawatPosted Oct 23, 2012, 8:29 PM EDT
Thanks Sukesh to share this. I have some question if you have time pls explain these for me: 1. Why are you using LayoutTemplate for customer ListView? 2. Can you explain please Layout template working because table is defined in Layout Template but Rows and Columns are defines in ItemTemplate for Address ListView?