How to Create Dynamic User Rights Menus in Asp.net?
Create Dynamic User Rights Menus in Asp.net
Dynamic User Rights is used to assign user permissions to the users of user asp.net application. By using this code only the assigned menu options will be displayed to the users. In order create we have to use three database tables named UserMaster, Menumaster and UserRights. UserMaster table is used to store the user details when the user register with the site. Menumaster table is used to store the menu details like menu name, url, css class, image, etc. Similarly, UserRights table is used to store the assigned rights of user option.
MenuMaster Table : the structure of MenuMater table.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MenuMaster](
[MenuId] [int] IDENTITY(1,1) NOT NULL,
[CodeId] [int] NOT NULL DEFAULT ((0)),
[Name] [char](50) NOT NULL DEFAULT (”),
[MenuUrl] [nvarchar](250) NOT NULL DEFAULT (”),
[CSSClass] [nvarchar](250) NOT NULL DEFAULT (”),
[ImageUrl] [nvarchar](250) NOT NULL DEFAULT (”),
[ImageCSSClass] [nvarchar](250) NOT NULL DEFAULT (”),
[SrNo] [int] NOT NULL DEFAULT ((0)),
[Level] [int] NOT NULL DEFAULT ((0)),
[level2] [int] NOT NULL DEFAULT ((0)),
[AllowToUser] [bit] NOT NULL DEFAULT ((0)),
[EntryDate] [datetime] NOT NULL DEFAULT (getdate()),
[IsActive] [bit] NOT NULL DEFAULT ((1)),
[MenuDescription] [nvarchar](max) NOT NULL DEFAULT (”),
[MenuTooltip] [nvarchar](250) NOT NULL DEFAULT (”),
[Target] [nvarchar](50) NOT NULL DEFAULT (‘_self’),
CONSTRAINT [PK_MenuMaster] PRIMARY KEY CLUSTERED
(
[MenuId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]GO
SET ANSI_PADDING OFFUsersMaster Table : the structure of UsersMater table.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UsersMaster](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[Email] [nvarchar](250) NOT NULL DEFAULT (”),
[UserName] [nvarchar](250 NOT NULL DEFAULT (”),
[Password] [nvarchar](max) NOT NULL DEFAULT (”),
[IsActive] [bit] NOT NULL DEFAULT ((0)),
[IsAdmin] [bit] NOT NULL DEFAULT ((0)),
[UserGUID] [uniqueidentifier] NOT NULL CONSTRAINT DEFAULT (newid()),
[UserRoleID] [int] NOT NULL DEFAULT ((0)),
CONSTRAINT [PK_UsersMaster] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]UserRights Table : the structure of UserRights table.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserRights](
[Idno] [int] IDENTITY(1,1) NOT NULL,
[CodeId] [int] NOT NULL DEFAULT ((0)),
[Name] [char](50) NOT NULL DEFAULT (”),
[MenuUrl] [nvarchar](250) NOT NULL DEFAULT (”),
[CSSClass] [nvarchar](250) NOT NULL DEFAULT (”),
[ImageUrl] [nvarchar](250) NOT NULL DEFAULT (”),
[ImageCSSClass] [nvarchar](250) NOT NULL DEFAULT (”),
[SrNo] [int] NOT NULL DEFAULT ((0)),
[Level] [int] NOT NULL DEFAULT ((0)),
[level2] [int] NOT NULL DEFAULT ((0)),
[UserRoleID] [int] NOT NULL DEFAULT ((0)),
[AllowAdd] [bit] NOT NULL DEFAULT ((0)),
[AllowEdit] [bit] NOT NULL DEFAULT ((0)),
[AllowDelete] [bit] NOT NULL DEFAULT ((0)),
[AllowPrint] [bit] NOT NULL DEFAULT ((0)),
[AllowView] [bit] NOT NULL DEFAULT ((0)),
[EntryDate] [datetime] NOT NULL DEFAULT (getdate()),
[EntryBy] [int] NOT NULL DEFAULT (”),
[IsActive] [bit] NOT NULL DEFAULT ((1)),
[MenuDescription] [nvarchar](max) NOT NULL DEFAULT (”),
[MenuTooltip] [nvarchar](250) NOT NULL DEFAULT (”),
[UserID] [int] NOT NULL DEFAULT ((0)),
[Target] [nvarchar](50) NOT NULL DEFAULT (‘_self’),
CONSTRAINT [PK_UserRights] PRIMARY KEY CLUSTERED
(
[Idno] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]GO
SET ANSI_PADDING OFF
You can change the structure of the tables as per the requirements of your application. After creating the tables; let start working on aspx pages. Add the following code in you masterpage or mainpage which is common for all pages.
<div id=”Menu”>
<div class=”divMenu”>
<ul class=”ul1″>
<asp:DataList ID=”DataList1″ runat=”server” DataKeyField=”CodeId” DataSourceID=”SqlData” >
<ItemTemplate>
<asp:Label ID=”mnuCodeId” runat=”server” Text='<%# Eval(“CodeId”) %>’ Visible=”false” />
<li class=”li1″ id=”Li1″ runat=”server”>
<asp:Image ID=”Image1″ runat=”server” ImageUrl='<%# Eval(“ImageUrl”) %>’ CssClass='<%# Eval(“ImageCSSClass”) %>’ />
<asp:HyperLink ID=”HyperLink1″ Target='<%# Eval(“Target”) %>’ NavigateUrl='<%# Eval(“MenuUrl”) %>’ CssClass='<%# Eval(“CSSClass”) %>’ runat=”server” ToolTip='<%# Eval(“MenuTooltip”) %>’ Text ='<%# Eval(“Name”) %>’ />
</li>
<asp:DataList ID=”DataList2″ runat=”server” DataKeyField=”CodeId” DataSourceID=”SqlDataGroup2″ >
<ItemTemplate>
<li class=”li1″ id=”Li2″ style=”padding-left: 35px” runat=”server”>
<asp:Image ID=”Image1″ runat=”server” ImageUrl='<%# Eval(“ImageUrl”) %>’ CssClass='<%# Eval(“ImageCSSClass”) %>’ />
<asp:HyperLink ID=”HyperLink1″ Target='<%# Eval(“Target”) %>’ NavigateUrl='<%# Eval(“MenuUrl”) %>’ CssClass='<%# Eval(“CSSClass”) %>’ runat=”server” ToolTip='<%# Eval(“MenuTooltip”) %>’ Text ='<%# Eval(“Name”) %>’ />
</li></ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID=”SqlDataGroup2″ runat=”server” ConnectionString=”<%$ ConnectionStrings: SqlConnection %>”
SelectCommand='<%# GetselectString(Convert.ToInt32(Eval(“CodeId”)))%>’ >
</asp:SqlDataSource>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID=”SqlData” runat=”server” ConnectionString=”<%$ ConnectionStrings:SqlConnection %>” ></asp:SqlDataSource>
</ul>
</div>
</div>C# Code
protected void Page_Load(object sender, EventArgs e)
{
if (Session[User] == null)
{
Response.Redirect(Login.aspx);
}// if (!IsPostBack)
// {
SqlData.SelectCommand = this.GetMenuItems(1, 0);
// }
}
protected string GetselectString(int codeid)
{
return this.GetMenuItems(2,codeid);
}public string GetMenuItems(int level,int rootmenucodeid)
{
string str = “”;
str = “select * from sml_UserRights where level=” + level.ToString() + ” and level2 =” + rootmenucodeid.ToString() + ” and IsActive=1 and UserID =” + Session[“UserIDâ€].ToString() + ” order by SrNo”;return str;
}
Note : please use your own connection string from the web.config file for SqlDataSources.
I hope this will help
Leave a Reply