Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Data Driven Menu Control

Posted on 2006-06-14
1
533 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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