locked
The name 'menuBar' does not exist in the current context RRS feed

  • Question

  • User406384663 posted
    Hi I am new to asp.net and getting Error as "The name 'menuBar' does not exist in the current context"..I want to Create menu from database dynamically my Design code page is 


    <asp:Menu ID="menuBar" StaticEnableDefaultPopOutImage="False" runat="server" Orientation="Horizontal"
    BackColor="#666666" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em"
    ForeColor="White" StaticSubMenuIndent="10px">





    --------------**********************************Code Behind******************
    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class Home : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable menuData = null; try { menuData = new DataTable(); menuData = GetMenuData(); AddTopMenuItems(menuData); } catch (Exception ex) { Response.Write(ex.Message); } finally { menuData = null; } } } private DataTable GetMenuData() { try { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Vote"].ConnectionString)) { SqlCommand cmd = new SqlCommand("spMenuItem", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@UserID", "K010"); DataTable dtMenuItems = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dtMenuItems); cmd.Dispose(); sda.Dispose(); return dtMenuItems; } } catch (Exception ex) { Response.Write(ex.Message); } return null; } private void AddTopMenuItems(DataTable menuData) { DataView view = null; try { view = new DataView(menuData); view.RowFilter = "ParentID IS NULL"; foreach (DataRowView row in view) { //Adding the menu item MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString()); menuBar.Items.Add(newMenuItem); AddChildMenuItems(menuData, newMenuItem); } } catch (Exception ex) { Response.Write(ex.Message); } finally { view = null; } } private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem) { DataView view = null; try { view = new DataView(menuData); view.RowFilter = "ParentID=" + parentMenuItem.Value; foreach (DataRowView row in view) { MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString()); newMenuItem.NavigateUrl = row["NavigateUrl"].ToString(); parentMenuItem.ChildItems.Add(newMenuItem); AddChildMenuItems(menuData, newMenuItem); } } catch (Exception ex) { Response.Write(ex.Message); } finally { view = null; } } }

    Sunday, January 29, 2017 7:54 AM

All replies

  • User-1838255255 posted

    Hi Nikh,

    According to your description, as far as I know, this error may be caused by Page.aspx file not match Page.aspx.cs file, so my suggestion is: please Check your code behind file name and Inherits property on the @Page directive, make sure they both match. If this problem not solved, please let me know!

    Best Regards,

    Eric Du

    Monday, January 30, 2017 2:29 AM