The following is my menu item data table in design mode:

sql database

The following is the script of my table:

  1. CREATE TABLE [dbo].[tbl_Menu](
  2. [Menu_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Menu_ParentID] [int] NULL,
  4. [Menu_Name] [varchar](50) NULL,
  5. [Menu_Page_URL] [varchar](500) NULL,
  6. CONSTRAINT [PK_tbl_Menu] PRIMARY KEY CLUSTERED
  7. (
  8. [Menu_ID] ASC
  9. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  10. ) ON [PRIMARY]
  11. GO

Now the data in my tbl_Menu table:

tbl_menu table

Here in this application I am using a Master Pager to provide a consistent look in the entire application. So I wrote code to generate a menu item from the database in MasterPage.master.cs.

The following is my MasterPage.Master:

  1. <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MenuFromDB.MasterPage" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <link href="StyleSheetMenu.css" rel="stylesheet" />
  7. <link href="StyleSheet.css" rel="stylesheet" />
  8. <asp:ContentPlaceHolder ID="head" runat="server">
  9. </asp:ContentPlaceHolder>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <div>
  14. <table cellpadding="1" cellspacing="1" width="880px" align="center" class="BlueBorder"
  15. style="background: White;">
  16. <tr>
  17. <td style="height: 100px; background-color: skyblue; padding-left: 10px;">
  18. <span style="font-size: 20pt; font-weight: bold; color: blue;">Data Base Driven Menu Item In ASP.NET C#</span>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td style="background-color: orange; padding-left: 1px;">
  23. <asp:Menu ID="MenuFromDB" runat="server" Orientation="Horizontal">
  24. <levelmenuitemstyles>
  25. <asp:menuitemstyle cssclass="Parent_Menu" />
  26. <asp:menuitemstyle cssclass="level_menu" />
  27. </levelmenuitemstyles>
  28. <staticselectedstyle cssclass="selected" />
  29. </asp:Menu>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td style="padding-top: 40px; padding-bottom: 80px; padding-left: 10px">
  34. <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  35. </asp:ContentPlaceHolder>
  36. </td>
  37. </tr>
  38. </table>
  39. </div>
  40. </form>
  41. </body>
  42. </html>
Now the MasterPage.Master.cs is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Configuration;
  11. namespace MenuFromDB
  12. {
  13. public partial class MasterPage : System.Web.UI.MasterPage
  14. {
  15. SqlDataAdapter da;
  16. DataSet ds = new DataSet();
  17. DataTable dt = new DataTable();
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. if (!Page.IsPostBack)
  21. {
  22. dt = GetMenuDataFromDB(0);
  23. PopulateMenu(dt, 0, null);
  24. }
  25. }
  26. public DataTable GetMenuDataFromDB(int MenuParentID)
  27. {
  28. SqlConnection con = new SqlConnection();
  29. ds = new DataSet();
  30. con.ConnectionString = @"Data Source=MyPC\SqlServer2k8; Initial Catalog=Test; Integrated Security=true;";
  31. SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_Menu WHERE Menu_ParentID='" + MenuParentID + "'", con);
  32. da = new SqlDataAdapter(cmd);
  33. da.Fill(ds);
  34. con.Open();
  35. cmd.ExecuteNonQuery();
  36. con.Close();
  37. return ds.Tables[0];
  38. }
  39. private void PopulateMenu(DataTable dt, int Menu_Parent_ID, MenuItem Parent_MenuItem)
  40. {
  41. string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
  42. foreach (DataRow row in dt.Rows)
  43. {
  44. MenuItem menuItem = new MenuItem
  45. {
  46. Value = row["Menu_Id"].ToString(),
  47. Text = row["Menu_Name"].ToString(),
  48. NavigateUrl = row["Menu_Page_URL"].ToString(),
  49. Selected = row["Menu_Page_URL"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
  50. };
  51. if (Menu_Parent_ID == 0)
  52. {
  53. MenuFromDB.Items.Add(menuItem);
  54. DataTable dtChildMenu = new DataTable();
  55. dtChildMenu = this.GetMenuDataFromDB(int.Parse(menuItem.Value));
  56. PopulateMenu(dtChildMenu, int.Parse(menuItem.Value), menuItem);
  57. }
  58. else
  59. {
  60. Parent_MenuItem.ChildItems.Add(menuItem);
  61. }
  62. }
  63. }
  64. }
  65. }

Now run the application.

demo application

report menuitem

Now hover on any menu and see the child menu.

aboutus menuitem

social tab menuitem