Create table for menu
CREATE TABLE [dbo].[tblMenuMaster]( [MenuID] [int] IDENTITY(1,1) NOT NULL, [MenuName] [varchar](100) NULL, [DisplayOrder] [int] NULL, PRIMARY KEY CLUSTERED ( [MenuID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Create another table for the submenu
CREATE TABLE [dbo].[tblSubMenuMaster]( [SubMenuID] [int] IDENTITY(1,1) NOT NULL, [MenuID] [int] NULL, [SubMenuName] [varchar](100) NULL, [MainMenuDisplayOrder] [int] NULL, [DisplayOrder] [int] NULL, [SubMenuUrl] [varchar](500) NULL, [VisibleInMenu] [bit] NULL, PRIMARY KEY CLUSTERED ( [SubMenuID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Now go to the main page. HTML code:
<div class="menubar"> <%--<ul class="tabs">--%> <asp:Literal ID="ltMenus" runat="server"></asp:Literal> <%--</ul>--%> </div>
Code by code:
private void GenerateMenus() { clsMenu obj = new clsMenu(); System.Data.DataSet ds = new System.Data.DataSet(); String PageName = ""; PageName = Path.GetFileName(Page.AppRelativeVirtualPath); ds = obj.GetMenusByRole(GetRoleId(), PageName); StringBuilder sb = new StringBuilder("<ul class='tabs'>"); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { sb.Append(String.Format("<li class='{0}'><a rel='{1}' href='{1}' > {2} </a> ", Convert.ToString(row["css"]), ResolveUrl(Convert.ToString(row["PagePath"])), Convert.ToString(row["MenuName"]))); //sb.Append(String.Format("<li '><a rel='{0}' href='{0}' > {1} </a> ", ResolveUrl(Convert.ToString(row["PagePath"])), Convert.ToString(row["MenuName"]))); System.Data.DataTable t = CCMMUtility.GetFilterDataforIntColumn("MenuID", Convert.ToString(row["MenuID"]), ds.Tables[1]); if (t.Rows.Count > 0) { sb.Append("<ul>"); for (int i = 0; i < t.Rows.Count; i++) { sb.Append(String.Format("<li><a href='{0}' class='dir' style='cursor: pointer;'>{1}</a></li>", ResolveUrl(Convert.ToString(t.Rows[i]["PagePath"])), Convert.ToString(t.Rows[i]["PageAliasName"]))); } sb.Append("</ul>"); } sb.Append("</li>"); } sb.Append("</ul>"); ltMenus.Text = sb.ToString(); }
he needs to save the proc to call the dynamic menu according to the Role identifier, as shown below
CREATE PROCEDURE [dbo].[proc_GetMenusByRole] ( @RoleId int, @PageName varchar(100) ) AS SET NOCOUNT ON; SELECT mm.MenuID, mm.MenuName,dbo.Extract_CssNameForMenuByMenuIDAndPageName(mm.MenuID, @PageName) as css ,dbo.proc_Extract_MenuPageByRoleIDAndMenuID(@RoleId, mm.MenuID) as PagePath , mm.DisplayOrder FROM tblMenuMaster mm WHERE mm.MenuID IN (SELECT s.MenuID from tblSiteRolePermissions p INNER JOIN tblSitePages s ON p.fkSitePageId = s.pkSitePageId WHERE (p.fkRoleId = @RoleId and p.ViewOnly=1)) Union All select 0 as menuid ,'Change Password' as MenuName, case @pagename when 'ChangePassword.aspx' then 'active' else '' end as css,'~/User/ChangePassword.aspx' as PagePath, 10000 as Displayorder ORDER BY DisplayOrder SELECT s.MenuID, s.pkSitePageId, s.PageAliasName, s.SitePageName,s.pagepath from tblSiteRolePermissions p INNER JOIN tblSitePages s ON p.fkSitePageId = s.pkSitePageId WHERE (p.fkRoleId =@RoleId and p.ViewOnly=1) ORDER BY s.pkSitePageId
// a new sp is launched here
CREATE function [dbo].[Extract_CssNameForMenuByMenuIDAndPageName](@MenuID int, PageName varchar(100)) returns nvarchar(50) as begin declare @result nvarchar(50) set @result = '' IF EXISTS (SELECT pkSitePageId FROM tblsitepages WHERE (MenuID = @MenuID) AND (UPPER(SitePageName) = @PageName)) BEGIN SET @result = 'active' END return @result end
// use another sp
CREATE function [dbo].[proc_Extract_MenuPageByRoleIDAndMenuID] (@RoleId int, @MenuID int) returns nvarchar(500) as begin declare @result nvarchar(500) SELECT top 1 @result = s.pagepath FROM tblSitePages AS s INNER JOIN tblSiteRolePermissions AS p ON s.pkSitePageId = p.fkSitePageId WHERE (p.fkRoleId = @RoleId) AND (s.MenuID = @MenuID) and p.ViewOnly=1 ORDER BY s.pkSitePageId return @result end
This is just a way to do it, you can change it according to your requirement .........
proc_Extract_MenuPageByRoleIDAndMenuID sp is used to get the name of the page and its path,
Extract_CssNameForMenuByMenuIDAndPageName sp is used to set the class active to the first li means the first menu. Hop this will help you ..... Its a working code.