Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. dynamic menu...

dynamic menu...

Scheduled Pinned Locked Moved ASP.NET
tutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RajpootRohan
    wrote on last edited by
    #1

    Hi to all, I am working on a shopping cart project. Here the scenerio is, if the admin adds a new category, that should be shown in the menu. If the admin adds a new sub category , then it should be displayed as the child item of the main category. Can anybody suggest me how to achieve this. cheers sneha

    cheers, sneha

    C 1 Reply Last reply
    0
    • R RajpootRohan

      Hi to all, I am working on a shopping cart project. Here the scenerio is, if the admin adds a new category, that should be shown in the menu. If the admin adds a new sub category , then it should be displayed as the child item of the main category. Can anybody suggest me how to achieve this. cheers sneha

      cheers, sneha

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A dynamic menu will be done using javascript. Just look at any of the menus that exist on the web and work out how to make one work using dynamic properties instead of a static file, or even just write that file as needed.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      R 1 Reply Last reply
      0
      • C Christian Graus

        A dynamic menu will be done using javascript. Just look at any of the menus that exist on the web and work out how to make one work using dynamic properties instead of a static file, or even just write that file as needed.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        R Offline
        R Offline
        RajpootRohan
        wrote on last edited by
        #3

        Thank you for replying. I did like this:

        using System;
        using System.Configuration;
        using System.Collections;
        using System.Data;
        using System.Data.SqlClient;
        using System.Linq;
        using System.Web;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.UI.HtmlControls;
        using System.Web.UI.WebControls;
        using System.Web.UI.WebControls.WebParts;
        using System.Xml.Linq;

        public partial class _Default : System.Web.UI.Page
        {
        string name;
        protected void Page_Load(object sender, EventArgs e)
        {
        if(!Page.IsPostBack)
        {
        populate_menu();
        }
        }

        DataSet GetMenuData()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings\["newcon"\].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            ArrayList arl = new ArrayList();
            cmd.Connection = con;
            cmd.CommandText = "select \* from CATEGORY ";
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            cmd.Connection.Open();
            sda.Fill(ds, "CATEGORY");
            return ds;
        }
        
        public void populate\_menu()
        {
            DataSet ds = GetMenuData();
            foreach(DataRow drow in ds.Tables\["CATEGORY"\].Rows)
            {
                MenuItem masteritem = new MenuItem((string)drow\["category\_name"\]);
                name = masteritem.Text;
                get\_id();
                int par\_id = Convert.ToInt32 (ViewState\["p\_id"\]);
                if (par\_id == 0)
                {
                    Menu1.Items.Add(masteritem);
                }
                else
                {
                    //masteritem.ChildItems.Add(masteritem);
                    Menu1.Items.Add(masteritem);
                }
            }
        }
        
        public void get\_id()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings\["newcon"\].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select parent\_id from CATEGORY where category\_name='"+name+"'";
            cmd.Connection.Open();
            int pa\_id = Convert.ToInt32(cmd.ExecuteScalar());
            ViewState\["p\_id"\] = pa\_id;
        
        }
        

        }

        From this I am getting output like this: Catering Plastics Glassware

        cheers, sneha

        C 1 Reply Last reply
        0
        • R RajpootRohan

          Thank you for replying. I did like this:

          using System;
          using System.Configuration;
          using System.Collections;
          using System.Data;
          using System.Data.SqlClient;
          using System.Linq;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.HtmlControls;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Xml.Linq;

          public partial class _Default : System.Web.UI.Page
          {
          string name;
          protected void Page_Load(object sender, EventArgs e)
          {
          if(!Page.IsPostBack)
          {
          populate_menu();
          }
          }

          DataSet GetMenuData()
          {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings\["newcon"\].ConnectionString);
              SqlCommand cmd = new SqlCommand();
              ArrayList arl = new ArrayList();
              cmd.Connection = con;
              cmd.CommandText = "select \* from CATEGORY ";
              SqlDataAdapter sda = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              cmd.Connection.Open();
              sda.Fill(ds, "CATEGORY");
              return ds;
          }
          
          public void populate\_menu()
          {
              DataSet ds = GetMenuData();
              foreach(DataRow drow in ds.Tables\["CATEGORY"\].Rows)
              {
                  MenuItem masteritem = new MenuItem((string)drow\["category\_name"\]);
                  name = masteritem.Text;
                  get\_id();
                  int par\_id = Convert.ToInt32 (ViewState\["p\_id"\]);
                  if (par\_id == 0)
                  {
                      Menu1.Items.Add(masteritem);
                  }
                  else
                  {
                      //masteritem.ChildItems.Add(masteritem);
                      Menu1.Items.Add(masteritem);
                  }
              }
          }
          
          public void get\_id()
          {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings\["newcon"\].ConnectionString);
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.CommandText = "select parent\_id from CATEGORY where category\_name='"+name+"'";
              cmd.Connection.Open();
              int pa\_id = Convert.ToInt32(cmd.ExecuteScalar());
              ViewState\["p\_id"\] = pa\_id;
          
          }
          

          }

          From this I am getting output like this: Catering Plastics Glassware

          cheers, sneha

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Looks about right. You're not adding subitems, you're adding all your items to the one menu

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          R 1 Reply Last reply
          0
          • C Christian Graus

            Looks about right. You're not adding subitems, you're adding all your items to the one menu

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            R Offline
            R Offline
            RajpootRohan
            wrote on last edited by
            #5

            Hi,

            public void populate\_menu()
            {
                DataSet ds = GetMenuData();
                foreach(DataRow drow in ds.Tables\["CATEGORY"\].Rows)
                {
                    MenuItem masteritem = new MenuItem((string)drow\["category\_name"\]);
                    name = masteritem.Text;
                    get\_id();
                    par\_id = Convert.ToInt32 (ViewState\["p\_id"\]);
                    if (par\_id == 0)
                    {
                        Menu1.Items.Add(masteritem);
                    }
                    else
                    {
                        //now find the category whose category\_id is par\_id
                        get\_category\_name();
                        ca\_name = ViewState\["cname"\].ToString();
                        
            
                        //then under that category, add this sub category
                        Menu1.Items**\[0\]**.ChildItems.Add(masteritem);
                    }
                }
            }
            

            In this I am unable to find the menu index of a particular category. In this I just tried by giving it as "0". I want the menuindex based on the category name "ca_name".

            cheers, sneha

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups