We will learn here how to create a dynamic menu control with data without a database.

Initial Chamber

Step 1

Open your Visual Studio and create an empty website then provide a suitable name such as DynamicMenu.aspx.

Step 2

In Solution Explorer you will get your empty website, then add some web forms.

DynamicMenu (your empty website). Right-click and select Add New Item Web Form. Name it DynamicMenu.aspx.

Design Chamber

Step 3

Open the DynamicMenu.aspx file and write some code for the design of the application.

Step 3.1

Use the following stylesheet code in the head chamber of the page like:

  1. <style type="text/css">
  2. body
  3. {
  4. background-color:mediumaquamarine;
  5. font-family: Arial;
  6. font-size: 10pt;
  7. color: #444;
  8. }
  9. .ParentMenu, .ParentMenu:hover {
  10. width: 100px;
  11. background-color: #fff;
  12. color: #333;
  13. text-align: center;
  14. height: 30px;
  15. line-height: 30px;
  16. margin-right: 5px;
  17. }
  18. .ParentMenu:hover {
  19. background-color: #ccc;
  20. }
  21. .ChildMenu, .ChildMenu:hover {
  22. width: 110px;
  23. background-color: #fff;
  24. color: #333;
  25. text-align: center;
  26. height: 30px;
  27. line-height: 30px;
  28. margin-top: 5px;
  29. }
  30. .ChildMenu:hover {
  31. background-color: #ccc;
  32. }
  33. .selected, .selected:hover {
  34. background-color: #A6A6A6 !important;
  35. color: #fff;
  36. }
  37. .level2 {
  38. background-color: #fff;
  39. }
  40. </style>

Step 3.2

Choose menu control from the toolbox and use the following in your design page:

  1. <div>
  2. <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
  3. <LevelMenuItemStyles>
  4. <asp:MenuItemStyle CssClass="ParentMenu" />
  5. <asp:MenuItemStyle CssClass="ChildMenu" />
  6. <asp:MenuItemStyle CssClass="ChildMenu" />
  7. </LevelMenuItemStyles>
  8. <StaticSelectedStyle CssClass="selected" />
  9. </asp:Menu>
  10. </div>
Now the page looks as in the following after designing your page.

DynamicMenu.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicMenu.aspx.cs" Inherits="DynamicMenu" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Dynamic Menu Control Article for C# Corner by Upendra Pratap Shahi</title>
  6. <style type="text/css">
  7. body {
  8. background-color: mediumaquamarine;
  9. font-family: Arial;
  10. font-size: 10pt;
  11. color: #444;
  12. }
  13. .ParentMenu, .ParentMenu:hover {
  14. width: 100px;
  15. background-color: #fff;
  16. color: #333;
  17. text-align: center;
  18. height: 30px;
  19. line-height: 30px;
  20. margin-right: 5px;
  21. }
  22. .ParentMenu:hover {
  23. background-color: #ccc;
  24. }
  25. .ChildMenu, .ChildMenu:hover {
  26. width: 110px;
  27. background-color: #fff;
  28. color: #333;
  29. text-align: center;
  30. height: 30px;
  31. line-height: 30px;
  32. margin-top: 5px;
  33. }
  34. .ChildMenu:hover {
  35. background-color: #ccc;
  36. }
  37. .selected, .selected:hover {
  38. background-color: #A6A6A6 !important;
  39. color: #fff;
  40. }
  41. .level2 {
  42. background-color: #fff;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <form id="form1" runat="server">
  48. <div>
  49. <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
  50. <LevelMenuItemStyles>
  51. <asp:MenuItemStyle CssClass="ParentMenu" />
  52. <asp:MenuItemStyle CssClass="ChildMenu" />
  53. <asp:MenuItemStyle CssClass="ChildMenu" />
  54. </LevelMenuItemStyles>
  55. <StaticSelectedStyle CssClass="selected" />
  56. </asp:Menu>
  57. </div>
  58. </form>
  59. </body>
  60. </html>
design
Figure 1: Design

Your design page looks as in above. But still your menu control is not yet designed. For this go further.

Code Chamber

Step 4

In the code chamber we will write some code so that our application works.

Adding the following namespaces in the namespace section of your code behind page.
  1. using System.IO;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
Now your page looks as in the following.

DynamicMenu.aspx.cs
  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. public partial class DynamicMenu : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (!IsPostBack)
  16. {
  17. DataTable dt = this.BindMenuData(0);
  18. DynamicMenuControlPopulation(dt, 0, null);
  19. }
  20. }
  21. /// <summary>
  22. /// This function retur a datatable according to parent menu id passed by user
  23. /// </summary>
  24. /// <param name="parentmenuId">parent menu ID</param>
  25. /// <returns>data table</returns>
  26. protected DataTable BindMenuData(int parentmenuId)
  27. {
  28. //declaration of variable used
  29. DataSet ds = new DataSet();
  30. DataTable dt;
  31. DataRow dr;
  32. DataColumn menu;
  33. DataColumn pMenu;
  34. DataColumn title;
  35. DataColumn description;
  36. DataColumn URL;
  37. //create an object of datatable
  38. dt=new DataTable();
  39. //creating column of datatable with datatype
  40. menu= new DataColumn("MenuId",Type.GetType("System.Int32"));
  41. pMenu=new DataColumn("ParentId", Type.GetType("System.Int32"));
  42. title=new DataColumn("Title",Type.GetType("System.String"));
  43. description=new DataColumn("Description",Type.GetType("System.String"));
  44. URL=new DataColumn("URL",Type.GetType("System.String"));
  45. //bind data table columns in datatable
  46. dt.Columns.Add(menu);//1st column
  47. dt.Columns.Add(pMenu);//2nd column
  48. dt.Columns.Add(title);//3rd column
  49. dt.Columns.Add(description);//4th column
  50. dt.Columns.Add(URL);//5th column
  51. //creating data row and assiging the value to columns of datatable
  52. //1st row of data table
  53. dr = dt.NewRow();
  54. dr["MenuId"] = 1;
  55. dr["ParentId"] = 0;
  56. dr["Title"] = "Home";
  57. dr["Description"] = "";
  58. dr["URL"] = "~/Home.aspx";
  59. dt.Rows.Add(dr);
  60. //2nd row of data table
  61. dr = dt.NewRow();
  62. dr["MenuId"] = 2;
  63. dr["ParentId"] = 0;
  64. dr["Title"] = "Customer Service";
  65. dr["Description"] = "Customer Service";
  66. dr["URL"] = "~/Customer.aspx";
  67. dt.Rows.Add(dr);
  68. //3rd row of data table
  69. dr = dt.NewRow();
  70. dr["MenuId"] = 3;
  71. dr["ParentId"] = 0;
  72. dr["Title"] = "About";
  73. dr["Description"] = "About us page";
  74. dr["URL"] = "~/AboutUs.aspx";
  75. dt.Rows.Add(dr);
  76. //4th row of data table
  77. dr = dt.NewRow();
  78. dr["MenuId"] = 4;
  79. dr["ParentId"] = 0;
  80. dr["Title"] = "Contact Us";
  81. dr["Description"] = "Contact Us page";
  82. dr["URL"] = "~/Contact.aspx";
  83. dt.Rows.Add(dr);
  84. //5th row of data table
  85. dr = dt.NewRow();
  86. dr["MenuId"] = 5;
  87. dr["ParentId"] = 0;
  88. dr["Title"] = "Testmonial";
  89. dr["Description"] = "Testimonial page";
  90. dr["URL"] = "~/Testimonial.aspx";
  91. dt.Rows.Add(dr);
  92. //6th row of data table
  93. dr = dt.NewRow();
  94. dr["MenuId"] = 6;
  95. dr["ParentId"] = 2;
  96. dr["Title"] = "Consulting";
  97. dr["Description"] = "Consulting page";
  98. dr["URL"] = "~/Consult.aspx";
  99. dt.Rows.Add(dr);
  100. //7th row of data table
  101. dr = dt.NewRow();
  102. dr["MenuId"] = 7;
  103. dr["ParentId"] = 2;
  104. dr["Title"] = "Outsourcing";
  105. dr["Description"] = "Outsourcing page";
  106. dr["URL"] = "~/Outsource.aspx";
  107. dt.Rows.Add(dr);
  108. //8th row of data table
  109. dr = dt.NewRow();
  110. dr["MenuId"] = 8;
  111. dr["ParentId"] = 7;
  112. dr["Title"] = "Domestic";
  113. dr["Description"] = "Domestic outsourcing page";
  114. dr["URL"] = "~/Domestic.aspx";
  115. dt.Rows.Add(dr);
  116. //9th row of data table
  117. dr = dt.NewRow();
  118. dr["MenuId"] = 9;
  119. dr["ParentId"] = 7;
  120. dr["Title"] = "International";
  121. dr["Description"] = "International outsourcing page";
  122. dr["URL"] = "~/International.aspx";
  123. dt.Rows.Add(dr);
  124. ds.Tables.Add(dt);
  125. var dv = ds.Tables[0].DefaultView;
  126. dv.RowFilter = "ParentId='" + parentmenuId + "'";
  127. DataSet ds1 = new DataSet();
  128. var newdt = dv.ToTable();
  129. return newdt;
  130. }
  131. /// <summary>
  132. /// This is a recursive function to fetchout the data to create a menu from data table
  133. /// </summary>
  134. /// <param name="dt">datatable</param>
  135. /// <param name="parentMenuId">parent menu Id of integer type</param>
  136. /// <param name="parentMenuItem"> Menu Item control</param>
  137. protected void DynamicMenuControlPopulation(DataTable dt, int parentMenuId, MenuItem parentMenuItem)
  138. {
  139. string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
  140. foreach (DataRow row in dt.Rows)
  141. {
  142. MenuItem menuItem = new MenuItem
  143. {
  144. Value = row["MenuId"].ToString(),
  145. Text = row["Title"].ToString(),
  146. NavigateUrl = row["URL"].ToString(),
  147. Selected = row["URL"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
  148. };
  149. if (parentMenuId == 0)
  150. {
  151. Menu1.Items.Add(menuItem);
  152. DataTable dtChild = this.BindMenuData(int.Parse(menuItem.Value));
  153. DynamicMenuControlPopulation(dtChild, int.Parse(menuItem.Value), menuItem);
  154. }
  155. else
  156. {
  157. parentMenuItem.ChildItems.Add(menuItem);
  158. DataTable dtChild = this.BindMenuData(int.Parse(menuItem.Value));
  159. DynamicMenuControlPopulation(dtChild, int.Parse(menuItem.Value), menuItem);
  160. }
  161. }
  162. }
  163. }
Output

O level menu

Figure 2: 0-level menu

1 level menu

Figure 3: 1-level menu

2 level menu

Figure 4: 2-level menu

I hope you liked this. Have a good day. Thank you for reading.