Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have one navigationmenu on my masterpage. Whenever a user logins, the navigationmenu should display the menuitems as per the users permission.

eg: i have three navigationmenu items i.e. menuitem1, menuitem2, menuitem3.
a normal user has access only to two menuitems i.e. menuitem1 and menuitem2. And the third menuitems is either disable or hidden to him.

So when a user logins, he can only see two menuitems i.e. menuitem1 and menuitem2.
My Program: wht I did is I wrote user authentication code on my login.aspx page
i.e.
If lbl.Text = "user" Then
nwperm()
Response.Redirect("Home.aspx")
end if

HERE nwperm is a function which i wrote in the class file located in App_Code folder.
Sub nwperm()
Dim mymenu As Menu = Master.FindControl("NavigationMenu")
Dim mymi As MenuItem = mymenu.FindItem("TechnicalSupport")
mymi.Enabled =True
end sub

Problem/error: "object reference cannot create an instance of an object."
Please help me.
Posted
Updated 17-Jan-12 2:56am
v2
Comments
mirzamujib 17-Jan-12 9:10am    
need help guys....
manognya kota 17-Jan-12 13:46pm    
Is the class file a content web form? (Meaning is the file like aspx.cs which has inherits the master page)?
mirzamujib 18-Jan-12 1:37am    
no, it don't inherits the master page.
Thanks..
manognya kota 18-Jan-12 2:23am    
If it doesnt inherit ..it means that there is no menu object there..and hence the error.Correct me if iam wrong .Could you please give the page declarations of the aspx pages.

Hi,


This is my understanding from your description.

You have
1.Master page say Masterpage.master
2.Mainmenu custom control on the master page. Say mainmenu.ascx
3.A login.aspx and and home.aspx

Try putting your code in mainmenu.ascx.cs file for enabling and disabling you menu according to the users.

And for the error, check whether the class file inherits the master page with the main menu.

See the below code for page declaratives and this works abolutely fine for me.
XML
Page declarative of master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="myMasterPage.master.cs" Inherits="myMasterPage" %>

<%@ Register TagName="myMainMenu" Src="~/myMainMenu.ascx" %>


Page declarative of home page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"
    MasterPageFile="~/myMasterPage.master" %>

<%@ MasterType VirtualPath="~/myMasterPage.master" %>

Page declarative of mainmenu
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MainMenu.ascx.cs" Inherits="MainMenu" %>

In your case, as the lanuguage is VB.You will have VB there.


For comparing your session variable for role, please see that the if condition first checks for the not null/nothing and the session value.

Hope this helps.
 
Share this answer
 
v3
Comments
mirzamujib 18-Jan-12 2:02am    
Thanks kota,
I tried to write the code in my mainmenu.acx.cs page. But it wont help me.
can you suggest me how can i make possible to user has menuitems according to his permissions
Thanks again...
manognya kota 18-Jan-12 2:21am    
when the user logs in , save the role of the user in a session variable.
According to the role you can enable/disable the menu items.
manognya kota 18-Jan-12 2:22am    
-"I tried to write the code in my mainmenu.acx.cs page. But it wont help me."

Dint get you.Please elaborate.
mirzamujib 18-Jan-12 3:26am    
thanks for your reply...
Actually i wrote the user authenticaion code on my login.aspx page and make a funtion in the class file which is stored in App-Code and calling that function from my login.aspx page.

In funtion i wrote the code following
Dim mymenu As Menu = Master.FindControl("NavigationMenu")
Dim mymi As MenuItem = mymenu.FindItem("TechnicalSupport")
mymi.Enabled = True
Why I wrote this code in class file becuase I want to call the above funtion whenever a user logins, so according to the appropriate users permission, menutimes of navigation menu disable/hide.

Thanks again
manognya kota 18-Jan-12 3:30am    
Try putting this code

Dim mymenu As Menu = Master.FindControl("NavigationMenu")
Dim mymi As MenuItem = mymenu.FindItem("TechnicalSupport")
mymi.Enabled = True

in your main menu.ascx.cs pageload.
I think you are not much clear about it. What is your current Datasource to populate the Menu? Best approach is to allow menu item in DB according to User roles and fetch item based on logged in user role.

If you have hard coded approach then show/hide menu item when you are building it instead of find the control after the rendered on client side.

But if you still thinking to use same above approach then find the exact menu item id from View source page then try to find it.

Thanks
 
Share this answer
 
Comments
mirzamujib 18-Jan-12 1:40am    
Thanks for the reply...
I'll try it and let you know..
mirzamujib 18-Jan-12 2:28am    
Hi,
I'm getting the error on my class file at line
Dim mymi As MenuItem = mymenu.FindItem("mymenuitemname")
Here error is " object reference not set to an instance of an object"
plz help me on this issue....

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