ARTICLE

Nested ListView in ASP.NET

Posted by Sukesh Marla Articles | ASP.NET Controls in C# September 28, 2012
In this article we will try to understand how to create a nested ListView in ASP.Net 3.5.
Reader Level:
Download Files:
 

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="Customer1@Customer.com",
                    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="Customer2@Customer.com",
                    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="Customer3@Customer.com",
                    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();

Login to add your contents and source code to this article
post comment
     

great article.....Thankshttp://www.microsoft-csharp-tutorials.com/post/2012/12/23/Listview-Control-in-Net-ItemInsert-in-Listview-ItemTemplate-and-AlternatingItemTemplate-in-Listview

Posted by Rajendar reddy Jan 06, 2013

super work

Posted by Vithal Wadje Jan 01, 2013

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

Posted by Mustafa Iqbal Oct 31, 2012

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?

Posted by Sandeep Singh Shekhawat Oct 24, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts