This blog will show you how you can bind ASP.NET menu control, using C# .NET from the data set. Thus, for this blog, first we will create a new ASP.NET Application and add the code given below into the page.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Menu.aspx.cs" Inherits="WebApplication7.Menu" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Bind Asp.net Menu Control Using C#</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <b>ASP.NET Menu Control</b>
  11. <asp:Menu ID="Menu1" runat="server" BackColor="#F7F6F3"
  12. DynamicHorizontalOffset="2" Width="50%"
  13. Font-Names="Verdana" Font-Size="0.9em" ForeColor="#7C6F57"
  14. StaticSubMenuIndent="10px" StaticEnableDefaultPopOutImage="false" Orientation="Horizontal">
  15. <StaticSelectedStyle BackColor="#5D7B9D" />
  16. <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
  17. <DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />
  18. <DynamicMenuStyle BackColor="#F7F6F3" />
  19. <DynamicSelectedStyle BackColor="#5D7B9D" />
  20. <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
  21. <StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />
  22. </asp:Menu>
  23. </div>
  24. </form>
  25. </body>
  26. </html>
Afterwards, write the code to bind the menu item from the backend code. For this, we will create a data set to bind it to the menu control or we will create a new data set function.
  1. public DataSet GetData()
  2. {
  3. // In order to test, we use the memory tables as the datasource.
  4. DataTable mainTB = new DataTable();
  5. DataColumn mainIdCol = new DataColumn("mainId");
  6. DataColumn mainNameCol = new DataColumn("mainName");
  7. DataColumn mainUrlCol = new DataColumn("mainUrl");
  8. mainTB.Columns.Add(mainIdCol);
  9. mainTB.Columns.Add(mainNameCol);
  10. mainTB.Columns.Add(mainUrlCol);
  11. DataTable childTB = new DataTable();
  12. DataColumn childIdCol = new DataColumn("childId");
  13. DataColumn childNameCol = new DataColumn("childName");
  14. // The MainId column of the child table is the foreign key to the main table.
  15. DataColumn childMainIdCol = new DataColumn("MainId");
  16. DataColumn childUrlCol = new DataColumn("childUrl");
  17. childTB.Columns.Add(childIdCol);
  18. childTB.Columns.Add(childNameCol);
  19. childTB.Columns.Add(childMainIdCol);
  20. childTB.Columns.Add(childUrlCol);
  21. // Insert some test records to the main table.
  22. DataRow dr = mainTB.NewRow();
  23. dr[0] = "1";
  24. dr[1] = "Home";
  25. dr[2] = "Test.aspx";
  26. mainTB.Rows.Add(dr);
  27. DataRow dr1 = mainTB.NewRow();
  28. dr1[0] = "2";
  29. dr1[1] = "Articles";
  30. dr1[2] = "Test.aspx";
  31. mainTB.Rows.Add(dr1);
  32. DataRow dr2 = mainTB.NewRow();
  33. dr2[0] = "3";
  34. dr2[1] = "Help";
  35. dr2[2] = "Test.aspx";
  36. mainTB.Rows.Add(dr2);
  37. DataRow dr3 = mainTB.NewRow();
  38. dr3[0] = "4";
  39. dr3[1] = "DownLoad";
  40. dr3[2] = "Test.aspx";
  41. mainTB.Rows.Add(dr3);
  42. // Insert some test records to the child table
  43. DataRow dr5 = childTB.NewRow();
  44. dr5[0] = "1";
  45. dr5[1] = "ASP.NET";
  46. dr5[2] = "2";
  47. dr5[3] = "Test.aspx";
  48. childTB.Rows.Add(dr5);
  49. DataRow dr6 = childTB.NewRow();
  50. dr6[0] = "2";
  51. dr6[1] = "SQL Server";
  52. dr6[2] = "2";
  53. dr6[3] = "Test.aspx";
  54. childTB.Rows.Add(dr6);
  55. DataRow dr7 = childTB.NewRow();
  56. dr7[0] = "3";
  57. dr7[1] = "JavaScript";
  58. dr7[2] = "2";
  59. dr7[3] = "Test.aspx";
  60. childTB.Rows.Add(dr7);
  61. // Use the DataSet to contain that two tables.
  62. DataSet ds = new DataSet();
  63. ds.Tables.Add(mainTB);
  64. ds.Tables.Add(childTB);
  65. // Build the relation between the main table and the child table.
  66. ds.Relations.Add("Child", ds.Tables[0].Columns["mainId"], ds.Tables[1].Columns["MainId"]);
  67. return ds;
  68. }
In the function, given above, we have to create a date set for the menu. Now, check the function, given below, to bind it with the menu.
  1. public void GenerateMenuItem()
  2. {
  3. // Get the data from database.
  4. DataSet ds = GetData();
  5. foreach (DataRow mainRow in ds.Tables[0].Rows)
  6. {
  7. // Load the records from the main table to the menu control.
  8. MenuItem masterItem = new MenuItem(mainRow["mainName"].ToString());
  9. masterItem.NavigateUrl = mainRow["mainUrl"].ToString();
  10. Menu1.Items.Add(masterItem);
  11. foreach (DataRow childRow in mainRow.GetChildRows("Child"))
  12. {
  13. // According to the relation of the main table and the child table, load the data from the child table.
  14. MenuItem childItem = new MenuItem((string)childRow["childName"]);
  15. childItem.NavigateUrl = childRow["childUrl"].ToString();
  16. masterItem.ChildItems.Add(childItem);
  17. }
  18. }
  19. }
Now, call the function, given above, on the page load.
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. GenerateMenuItem();
  6. }
  7. }
Now, we are done. Just run the Application to check the output.

output