Search code examples
c#asp.netlistviewnavbaritemtemplate

ERROR: the name 'lvSubCategories' does not exist in the current context


I am trying to show subcategories according to categories I am putting Listview inside another listview but the inner Listview throws an error in the cs file that the name 'lvSubCategories' does not exist in the current context.

Can listView not contain another listview inside itself?

Here is my Test.aspx markup:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="ShoppingHeart.Test" %>
    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="css/font-awesome.css" rel="stylesheet" />
        <link href="Scripts/bootstrap.css" rel="stylesheet" />
        <script src="Scripts/jquery-3.1.1.js"></script>
        <script src="Scripts/bootstrap.js"></script>
        <link href="Styling/TablesStyle.css" rel="stylesheet" />
        <link href="Styling/paneLStyle.css" rel="stylesheet" />
        <link href="Styling/NavBarStyle.css" rel="stylesheet" />
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <nav class="navbar navbar-default" style="background-color: rgba(154, 153, 153, 0.93)">
                    <div class="container">
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                            </button>
                            <asp:Image ID="Image1" Height="60px" Width="85px" ImageUrl="~/Images/FinalLogo.png"
                                runat="server" />
                        </div>
                        <div class="collapse navbar-collapse" id="myNavbar">

                            <ul class="nav navbar-nav">

                                <asp:ListView ID="lvCategories" runat="server">
                                    <ItemTemplate>
                                        <li class="dropdown">
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                                <%# Eval("CategoryName") %> <span class="caret"></span>
                                            </a>
                                            <asp:ListView ID="lvSubCategories" runat="server">
                                                <ItemTemplate>
                                                    <ul class="dropdown-menu">
                                                        <li><a href="#"><%# Eval("SubCategoryName") %></a></li>
                                                    </ul>
                                                </ItemTemplate>
                                            </asp:ListView>
                                        </li>

                                    </ItemTemplate>
                                </asp:ListView>
                            </ul>
                        </div>
                    </div>
                </nav>

                <asp:Button ID="btnLoadCategories" runat="server" Text="Load Categories"
                    OnClick="btnLoadCategories_Click" />
            </div>
        </form>
    </body>
    </html>

Here is my Test.aspx.cs code:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace ShoppingHeart
    {
        public partial class Test : System.Web.UI.Page
        {
            protected void btnLoadCategories_Click(object sender, EventArgs e)
            {
                var ds = new DataSet();
                var ds1 = new DataSet();

                string Categoryquery = "Select * from Category";
                string SubCategoryquery = "Select SubCategoryName from SubCategory where CategoryId = 1003";

                string ConString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString.ToString();

                using (var conn = new SqlConnection(ConString))
                {
                    var adpt = new SqlDataAdapter(Categoryquery, conn);
                    adpt.Fill(ds);

                    lvCategories.DataSource = ds;
                    lvCategories.DataBind();
                }

                using (var conn = new SqlConnection(ConString))
                {
                    var adpt = new SqlDataAdapter(SubCategoryquery, conn);
                    adpt.Fill(ds1);

                    lvSubCategories.DataSource = ds1;
                    lvSubCategories.DataBind();
                }
            }
        }
    }

Solution

  • Because it is a Nested ListView you do no have direct access. You need to use FindControl. That can be used in the OnItemDataBound event of the ListView

    <asp:ListView ID="lvCategories" runat="server" OnItemDataBound="lvCategories_ItemDataBound">
    
    protected void lvCategories_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        ListView lvSubCategories = e.Item.FindControl("lvSubCategories") as ListView;
    
        lvSubCategories.DataSource = source;
        lvSubCategories.DataBind();
    }
    

    Or by accessing the Item by Index.

    ListView lvSubCategories = lvCategories.Items[i].FindControl("lvSubCategories") as ListView;
    
    lvSubCategories.DataSource = source;
    lvSubCategories.DataBind();