---
title: "Nested ListView in Asp.Net"  
description: "Nested ListView in Asp.NetToday In this Article, I am trying to show you that how can we use nested list view control in asp.net. For example, suppo"  
author: "Sachindra Singh"  
published: 2011-02-14  
updated: 2020-06-10  
canonical: https://www.mindstick.com/articles/451/nested-listview-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Nested ListView in Asp.Net

## Nested ListView in Asp.Net

Today In this Article, I am trying to show you that how can we use nested list view control in asp.net. For example, suppose we want to display some categories and subcategories of any records. The [following demonstration](https://www.mindstick.com/articles/1110/select-insert-update-and-delete-using-stored-procedure-in-asp-dot-net-mvc4) will show you how to perform this task.\

##### Code: DemoListview.aspx

```
<asp:ListView runat="server" ID="ListView1" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">             <LayoutTemplate>                <table runat="server" id="table1" cellpadding="5" bgcolor="#E0E0E0" width="100">                    <tr runat="server" id="itemPlaceholder">                    </tr>                </table>            </LayoutTemplate>            <ItemTemplate><%-- sets the custom content for the data item in a ListView control.--%>                <tr id="Tr1" runat="server">                    <td id="Td1" runat="server" align="center">                        <asp:Label ID="NameLabel" runat="server" Font-Size="Large" Text='<%#Eval("Cat") %> ' />                        <hr />                        <asp:ListView ID="ListView2" runat="server" DataSourceID="SubCategory">                            <LayoutTemplate>                                <table runat="server" id="table2">                                    <tr runat="server" id="itemPlaceHolder">                                    </tr>                                </table>                            </LayoutTemplate>                            <ItemTemplate>                                <tr id="Tr2" runat="server">                                    <td id="Td2" runat="server" align="left">                                        <asp:Label ID="LabelSubCat" runat="server" Text='<%#Eval("SubCat")%>'                                    </td>                                </tr>                            </ItemTemplate>                        </asp:ListView>                        <asp:SqlDataSource ID="SubCategory" runat="server"></asp:SqlDataSource><%--SqlDataSource control use for database connectivity to retrieve SubCategory list --%>                    </td>                </tr>            </ItemTemplate>         </asp:ListView>        <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource><%--SqlDataSource control use for database connectivity to retrieve Category list--%>
```

#### Code: DemoListview.aspx.cs

```
public partial class DemolListview : System.Web.UI.Page{     protected void Page_Load(object sender, EventArgs e)    {         SqlDataSource1.ConnectionString = "Data Source=aaa; initial catalog = Detail; User id=bbb; password=cccc;";//creating connection from database          SqlDataSource1.SelectCommand = "Select * from Category";//Executing sql query    }     protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)    {         Label lblID = e.Item.FindControl("NameLabel") as Label;//finding control in Listview1 control         SqlDataSource sqlds = e.Item.FindControl("SubCategory") as SqlDataSource;//finding sqldatasource control in Listview1 control         sqlds.ConnectionString = " Data Source=aaa; initial catalog = Detail; User id=bbb; password=cccc;";//creating connection          sqlds.SelectCommand = "select SubCat from SubCategory where Cat='" + lblID.Text + "'";";//Executing sql query    }
```

When we will run the application it will show .Net and Database categories along with their subcategories as shown below:

#### Output

![Nested ListView in Asp.Net](https://www.mindstick.com/mindstickarticle/f05ac0cf-404e-436d-bed0-9fefdb4e0dee/images/633bc633-f793-415e-ae41-b30631b2697c.png)

---

Original Source: https://www.mindstick.com/articles/451/nested-listview-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
