Step
1:- Create table
CREATE TABLE MENU_T
(
MENU_ID INT IDENTITY,
MENU_NAME VARCHAR(MAX) NOT NULL,
MENU_LINK VARCHAR(MAX) NULL,
MENU_IMAGE VARCHAR(MAX) NULL,
MENU_DESCRIPTION VARCHAR(MAX) NULL,
MENU_PARENT_ID INT NULL,
DISPLAY_ORDER INT NOT NULL
)
Step
2:- Insert Header Menu Items
--Query
for adding Header Menu Items
INSERT INTO MENU_T(MENU_NAME,DISPLAY_ORDER)VALUES('MENU 1',10)
INSERT INTO MENU_T(MENU_NAME,DISPLAY_ORDER)VALUES('MENU 2',20)
INSERT INTO MENU_T(MENU_NAME,DISPLAY_ORDER)VALUES('MENU 3',30)
INSERT INTO MENU_T(MENU_NAME,DISPLAY_ORDER)VALUES('MENU 4',40)
Step
3:- Insert Child Menu Items
--Query
for adding Child Menu Items i.e. visible under "MENU 1"
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECTS','','','SCIENCE SUBJECT',1,11)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 1','SCIENCESUBJECT1.ASPX','','SCIENCE SUBJECT 1',1,12)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 2','SCIENCESUBJECT2.ASPX','','SCIENCE SUBJECT 2',1,13)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 3','SCIENCESUBJECT3.ASPX','','SCIENCE SUBJECT 3',1,14)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 4','SCIENCESUBJECT4.ASPX','','SCIENCE SUBJECT 4',1,15)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 5','SCIENCESUBJECT5.ASPX','','SCIENCE SUBJECT 5',1,16)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 6','SCIENCESUBJECT6.ASPX','','SCIENCE SUBJECT 6',1,17)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 7','SCIENCESUBJECT7.ASPX','','SCIENCE SUBJECT 7',1,18)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SCIENCE SUBJECT 8','SCIENCESUBJECT8.ASPX','','SCIENCE SUBJECT 8',1,19)
--Query
for adding Child Menu Items i.e visible under "MENU 2"
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECTS','','','COMMERCE SUBJECT',2,21)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 1','COMMERCESUBJECT1.ASPX','','COMMERCE SUBJECT 1',2,22)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 2','COMMERCESUBJECT2.ASPX','','COMMERCE SUBJECT 2',2,23)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 3','COMMERCESUBJECT3.ASPX','','COMMERCE SUBJECT 3',2,24)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 4','COMMERCESUBJECT4.ASPX','','COMMERCE SUBJECT 4',2,25)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 5','COMMERCESUBJECT5.ASPX','','COMMERCE SUBJECT 5',2,26)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 6','COMMERCESUBJECT6.ASPX','','COMMERCE SUBJECT 6',2,27)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 7','COMMERCESUBJECT7.ASPX','','COMMERCE SUBJECT 7',2,28)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('COMMERCE SUBJECT 8','COMMERCESUBJECT8.ASPX','','COMMERCE SUBJECT 8',2,29)
--Query
for adding Child Menu Items i.e visible under "MENU 3"
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECTS','','','ART SUBJECT',3,31)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 1','ARTSUBJECT1.ASPX','','ART SUBJECT 1',3,32)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 2','ARTSUBJECT2.ASPX','','ART SUBJECT 2',3,33)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 3','ARTSUBJECT3.ASPX','','ART SUBJECT 3',3,34)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 4','ARTSUBJECT4.ASPX','','ART SUBJECT 4',3,35)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 5','ARTSUBJECT5.ASPX','','ART SUBJECT 5',3,36)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 6','ARTSUBJECT6.ASPX','','ART SUBJECT 6',3,37)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 7','ARTSUBJECT7.ASPX','','ART SUBJECT 7',3,38)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('ART SUBJECT 8','ARTSUBJECT8.ASPX','','ART SUBJECT 8',3,39)
--Query
for adding Child Menu Items i.e visible under "MENU 4"
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('OTHER SUBJECTS','','','OTHER SUBJECT',4,41)
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('OTHER SUBJECT 1','','','OTHER SUBJECT 1',4,42)
Step
4:- Insert Sub Child Menu Items
--Query
for adding Sub Child Menu in Child Menu Items i.e visible under "MENU
4"
INSERT INTO MENU_T(MENU_NAME,MENU_LINK,MENU_IMAGE,MENU_DESCRIPTION,MENU_PARENT_ID,DISPLAY_ORDER)
VALUES('SUBJECT A','OTHERSUBJECTA.ASPX','','OTHER SUBJECT A',33,43)
Step
5:- Create Procedure
--Procedure
of get data
CREATE PROC getMENU
AS
BEGIN
SELECT * FROM MENU_T ORDER BY DISPLAY_ORDER
END
**********************************************************
Step
1:- MENU.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MENU.aspx.cs" Inherits="Demo.Menu" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.menuItem {
border: Solid 1px black;
width: 100%;
padding: 2px;
background-color: orange;
}
.menuItem a {
color: orange;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu
ID="Menu1"
Orientation="horizontal"
StaticMenuItemStyle-CssClass="menuItem"
DynamicMenuItemStyle-CssClass="menuItem"
runat="server">
</asp:Menu>
</div>
</form>
</body>
</html>
Step 2:- MENU.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Menu : Page
{
private SqlConnection _sqlConnection;
private SqlCommand _sqlCommand;
private SqlDataAdapter _sqlDataAdapter;
private DataSet _dataSet;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadMenu();
}
}
#region LoadMenu
protected void LoadMenu()
{
string _connection
= "server=ServerName; database=DatabaseName; uid=sa;
pwd=admin@123;";
_sqlConnection = new SqlConnection(_connection);
_sqlCommand = new SqlCommand
{
CommandText = "getMENU",
CommandType = CommandType.StoredProcedure,
Connection = _sqlConnection
};
_sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
_dataSet = new DataSet();
_sqlDataAdapter.Fill(_dataSet);
AddHeaderMenuItems(_dataSet.Tables[0]);
}
#endregion
#region AddHeaderMenuItems
protected void AddHeaderMenuItems(DataTable menuDataTable)
{
/*Filter
the data to get only the rows that have a null ParentID
(This will come on the Header menu items)*/
DataView view
= new DataView(menuDataTable);
view.RowFilter = "MENU_PARENT_ID IS NULL";
foreach (DataRowView row in view)
{
MenuItem headerMenuItem
= new MenuItem();
headerMenuItem.Text = row["MENU_NAME"].ToString();
headerMenuItem.Value = row["MENU_ID"].ToString();
headerMenuItem.ToolTip = row["MENU_DESCRIPTION"].ToString();
Menu1.Items.Add(headerMenuItem);
AddChildMenuItems(menuDataTable, headerMenuItem);
}
}
#endregion
#region AddChildMenuItems
protected void AddChildMenuItems(DataTable menuDataTable, MenuItem headerMenuItem)
{
DataView view
= new DataView(menuDataTable);
view.RowFilter = "MENU_PARENT_ID=" +
headerMenuItem.Value;
foreach (DataRowView row in view)
{
MenuItem childMenuItem
= new MenuItem();
childMenuItem.Text = row["MENU_NAME"].ToString();
childMenuItem.Value = row["MENU_ID"].ToString();
childMenuItem.ToolTip = row["MENU_DESCRIPTION"].ToString();
childMenuItem.NavigateUrl = row["MENU_LINK"].ToString();
headerMenuItem.ChildItems.Add(childMenuItem);
AddSubChildMenuItems(menuDataTable, childMenuItem);
}
}
#endregion
#region AddSubChildMenuItems
protected void AddSubChildMenuItems(DataTable menuDataTable, MenuItem childMenuItem)
{
DataView view
= new DataView(menuDataTable);
view.RowFilter = "MENU_PARENT_ID=" + childMenuItem.Value;
foreach (DataRowView row in view)
{
MenuItem subChildMenuItem
= new MenuItem();
subChildMenuItem.Text = row["MENU_NAME"].ToString();
subChildMenuItem.Value = row["MENU_ID"].ToString();
subChildMenuItem.ToolTip = row["MENU_DESCRIPTION"].ToString();
subChildMenuItem.NavigateUrl = row["MENU_LINK"].ToString();
childMenuItem.ChildItems.Add(subChildMenuItem);
}
}
#endregion
}
}
Menu
to menu example
Sub menu to menu example