Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys iam having requirement of enabling or disabling the menu items and submenu items in asp.net i've done that but when iam pasting the url of something that is not having the permission it is also populating in that role. so please help me
Posted

Try with below code:

Consider there are two roles Admin and User. Menu1 is ID of main menu and it contains 3 sub menus AdminReports, MasterReport, UserReport. Admin can see AdminReport and MasterReport but not UserReport. User role can see UserReport not other reports.
C#
protected void Page_Load(object sender, EventArgs e)
{
	// Get user role from session
	string role = Session["role"];
	if (role == "User")
	{
        // Disable the sub menu item
		MenuItem menuItem = Menu1.FindItem("AdminReport");		
		menuItem.Enabled = false;
		
		// Remove the sub menu item
		MenuItem menuItem1 = Menu1.FindItem("MasterReport");
		Menu1.Items.Remove(menuItem1);	
	}
	else if(role == "Admin")
	{
		MenuItem menuItem = Menu1.FindItem("UserReport"); 
		// Disable the sub menuitem
		menuItem.Enabled = false;			
	}
}
 
Share this answer
 
alright should be simple enough.
You have your method for checking if the user does have access to the user role it is provided?

Lets call it UserHasRole for the sake of the example.
UserHasRole returning a boolean value.

so in your aspx page try using codeBlocks.
HTML
<![CDATA[<% if (UserHasRole(userrole,userID))
{%>]]>
     Declare menuItem Here
<![CDATA[<%}%>]]>



UserHasRole might need to be declared in the codebehind of the page trying to access it. That is dependant on how your asp.net appplication in setup.
 
Share this answer
 
Thanks Manas_Kumar,but my Problem Here is i am getting role permissions from the database
so according to that menu items has to be displayed
for example
User1 have menu item access of 5,8,9,15 i.e, menu sub items (reports),analysis,adding,deleting etc,
user 2 have access for 10,20,30 menu items ie view, print, deploy etc
similarly the other users
so when ever user1 copying the 10 menuitem url in his address bar it is showing that page, i dont want to display likethat
so according to the database i must keep the menus visible to the users.

and i forget to mention that the menu is also displaying dynamically according to the type of user
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900