?
Solved

Data Driven Menu Control

Posted on 2006-06-14
1
Medium Priority
?
547 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
[X]
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
1 Comment
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 375 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

764 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