Solved

Data Driven Menu Control

Posted on 2006-06-14
1
529 Views
Last Modified: 2012-06-21
Does anyone have a good example of Data driven menu control.. The menu items should be loaded from the values in the database.. The example should include source code and should have been developed using C# and ASP.NET 2005

Thanks in Advance
0
Comment
Question by:Collindsouza
1 Comment
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 125 total points
ID: 16900864
I have created one recently. You use A DataSet (In which you can define relationships for foreign keys). I made two functions which makes it real easy to now contruct any menu, with however many Children Etc... Here is the full source code, with two functions and One Call in the Page Load.


//The below goes in the Page Load Event

        if (!IsPostBack)
        {
            PopulateMenu();
        }

//My function here gets the data from the DataBase and returns a DataSet to be used with the final function I will show further down

private DataSet GetDataSetForMenu()
{
  SqlDataAdapter adCat = new SqlDataAdapter("SELECT cat_id, cat_name, link_field, prod_counter FROM menu_creator ORDER BY cat_menu_order", Connection);
  SqlDataAdapter adProd = new SqlDataAdapter("SELECT cat_id, prod_count, prod_name, cat_name FROM start_menu_setup ORDER BY prod_name", Connection);

  DataSet ds = new DataSet();
  adCat.Fill(ds, "menu_creator");
  adProd.Fill(ds, "start_menu_setup");
  ds.Relations.Add("Children",
     ds.Tables["menu_creator"].Columns["cat_id"],
     ds.Tables["start_menu_setup"].Columns["cat_id"]);
  return ds;
}

//Finally this function populates the ASP.NET Menu.


  private void PopulateMenu()
  {

  DataSet ds = GetDataSetForMenu();

  foreach (DataRow parentItem in ds.Tables["menu_creator"].Rows)
  {
      if (Convert.ToInt32(parentItem["prod_counter"]) == 1)
      {
          MenuItem categoryItem = new MenuItem((string)parentItem["cat_name"]);
          categoryItem.NavigateUrl = "productdetail.aspx?prod_id=" + Convert.ToInt32(parentItem["link_field"]);
          Menu1.FindItem("Products").ChildItems.Add(categoryItem);
      }
      else
      {
          MenuItem categoryItem = new MenuItem((string)parentItem["cat_name"]);
          Menu1.FindItem("Products").ChildItems.Add(categoryItem);

          foreach (DataRow childItem in parentItem.GetChildRows("Children"))
          {
              MenuItem childrenItem = new MenuItem((string)childItem["prod_name"]);
              childrenItem.NavigateUrl = "productdetail.aspx?prod_id=" + Convert.ToInt32(childItem["prod_count"]);
              categoryItem.ChildItems.Add(childrenItem);
          }
      }
  }
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now