Thursday, February 9, 2012

ASP.NET Menu with Child Menu using C#.NET



Mostly you guys depend on css,jquery and template menu for your websites, but why not give ASP.NET Menu worth of trying .Below is my sample asp.net menu . Neat, Easy,Manageable and Fast to create . 



Steps :

1. drag and drop asp.net menu to your master page and name it Menu1 and set the Orientation="Horizontal".
   
2. On your code behind add all the parent menu item .

       ex:  MenuItem pParent1 = new menuitem("parent name","parent name","icon","url");
             MenuItem pParent2 = new menuitem("parent name","parent name","icon","url");
             MenuItem pParent3 = new menuitem("parent name","parent name","icon","url");
         
            MenuItem cChild1P1 = new menuitem("parent name","parent name","icon","url");
            MenuItem cChild2P1 = new menuitem("parent name","parent name","icon","url");
            MenuItem cChild3P1 = new menuitem("parent name","parent name","icon","url");


            MenuItem cChild1P2 = new menuitem("parent name","parent name","icon","url");
            MenuItem cChild2P2 = new menuitem("parent name","parent name","icon","url");

**the pParent refers to the Main Item
**while the cChild referes to the sub item

3. Add the Main Item in the Menu.

      Menu1.items.add(pParent1);
      Menu1.items.add(pParent2);
      Menu1.items.add(pParent3);
4. Add the sub items to the parent menu item .
   
      pParent.childitems.add(cChild1P1);

      pParent.childitems.add(cChild2P1);
      pParent.childitems.add(cChild3P1);


      pParent.childitems.add(cChild1P2);
      pParent.childitems.add(cChild2P2);


5. bind the Menu1.

    Menu1.DataBind();

cheers!