禁用/隐藏MasterPage的导航菜单项 [英] Disable/Hide Navigationmenu items of MasterPage

查看:130
本文介绍了禁用/隐藏MasterPage的导航菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的主页上有一个导航菜单.每当用户登录时,导航菜单应根据用户权限显示菜单项.

例如:我有三个导航菜单项,即menuitem1,menuitem2,menuitem3.
普通用户只能访问两个菜单项,即menuitem1和menuitem2.第三个菜单项对他来说是禁用或隐藏的.

因此,当用户登录时,他只能看到两个菜单项,即menuitem1和menuitem2.
我的程序:我是在我的login.aspx页上写了用户身份验证代码的吗?

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


这里nwperm是我在App_Code文件夹中的类文件中编写的函数.


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.

推荐答案




根据您的描述,这是我的理解.

你有
1.Master页面说Masterpage.master
2.主页上的Mainmenu自定义控件.说mainmenu.ascx
3.A login.aspx和home.aspx

尝试将代码放入mainmenu.ascx.cs文件中,以根据用户启用和禁用菜单.

并针对该错误检查类文件是否继承了带有主菜单的母版页.

有关页面声明的信息,请参见下面的代码,这对我来说绝对可以正常工作.
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.
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.



为了比较会话变量的角色,请注意if条件首先检查not null/nothing和会话值.

希望对您有所帮助.



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.


我认为您对此不太清楚.您当前要填充菜单的数据源是什么?最好的方法是根据用户角色允许数据库中的菜单项,并根据已登录的用户角色来获取菜单项.

如果您具有硬编码方法,则在构建菜单项时显示/隐藏菜单项,而不是在客户端呈现后找到控件.

但是,如果您仍在考虑使用与上述相同的方法,请从查看源代码"页面中找到确切的菜单项ID,然后尝试找到它.

谢谢
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


这篇关于禁用/隐藏MasterPage的导航菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆