articles

Home / DeveloperSection / Articles / Nested ListView in Asp.Net

Nested ListView in Asp.Net

Nested ListView in Asp.Net

Sachindra Singh 43338 14-Feb-2011

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


Updated 10-Jun-2020

Leave Comment

Comments

Liked By