如何在ASP.NET中登录页面后删除菜单 [英] How to remove menu after login page in ASP.NET

查看:87
本文介绍了如何在ASP.NET中登录页面后删除菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< asp:菜单ID =Menu1runat =serverwidth =200px>

<项目>

< asp:MenuItem Text =EMPLOYEE DETAILSNavigateUrl =〜/ Employee_Details.aspx

Value =Employee_Details>

< / asp:MenuItem>



< asp:MenuItem Text =HRNavigateUrl =〜/ HR.aspxValue =HR>



< asp:MenuItem Text =ManagerNavigateUrl =〜/ manager.aspxValue =Manager>











i想要在用户登录后显示员工菜单并删除人力资源和经理菜单。



注意一旦用户登录,它将重定向到主页。 home.aspx


home.aspx中的
所有菜单名称都可见。




用户登录主页后,
i尝试使用以下代码,仅显示员工菜单并删除人力资源和经理菜单。





在登录页面,我写下面的代码如下



if(txtuser.Text ==admin)

{

var menu = Page.Master.FindControl(Menu1)作为菜单;

if(menu!= null)

{

menu.Items.Remove(menu.FindItem(Attendance));

menu.Items.Remove(menu.FindItem(Manager));

}

}



来自上面的代码我正在取消考勤和人力资源菜单。



但是当用户登录home.aspx时,所有菜单都显示员工,出勤和经理。



我上面的鳕鱼有什么错误e。



我的尝试:



< asp:菜单ID =Menu1runat =serverwidth =200px>

<项目>

< asp:MenuItem Text =EMPLOYEE DETAILSNavigateUrl =〜/ Employee_Details.aspx

Value =Employee_Details>

< / asp:MenuItem>



< asp:MenuItem Text =HRNavigateUrl =〜/ HR.aspxValue =HR>



< asp:MenuItem Text =ManagerNavigateUrl =〜/ manager.aspxValue =Manager>











i想要在用户登录后显示员工菜单并删除人力资源和经理菜单。



注意一旦用户登录,它将重定向到主页。 home.aspx


home.aspx中的
所有菜单名称都可见。




用户登录主页后,
i尝试使用以下代码,仅显示员工菜单并删除人力资源和经理菜单。





在登录页面,我写下面的代码如下



if(txtuser.Text ==admin)

{

var menu = Page.Master.FindControl(Menu1)作为菜单;

if(menu!= null)

{

menu.Items.Remove(menu.FindItem(Attendance));

menu.Items.Remove(menu.FindItem(Manager));

}

}



来自上面的代码我正在取消考勤和人力资源菜单。



但是当用户登录home.aspx时,所有菜单都显示员工,出勤和经理。



上面代码中的错误是什么。

< asp:Menu id="Menu1" runat="server" width="200px" >
< Items >
< asp:MenuItem Text="EMPLOYEE DETAILS" NavigateUrl="~/Employee_Details.aspx"
Value="Employee_Details" >
< /asp:MenuItem >

<asp:MenuItem Text="HR" NavigateUrl="~/HR.aspx" Value="HR">

<asp:MenuItem Text="Manager" NavigateUrl="~/manager.aspx" Value="Manager">





i want to hide the HR and Manager menu once user login show the Employee menu and remove the HR and Manager menu.

Note once user login it will redirect to Home page. home.aspx

in home.aspx all the menu name are visible.


i tried the below code once user login in to home page shows the Employee menu only and remove the HR and Manager menu.


In Login page i written the below code as follows

if (txtuser.Text == "admin")
{
var menu = Page.Master.FindControl("Menu1") as Menu;
if (menu != null)
{
menu.Items.Remove(menu.FindItem("Attendance"));
menu.Items.Remove(menu.FindItem("Manager"));
}
}

from the above code i am removing the Attendance and HR menu.

but when user login the home.aspx all the menus are displaying Employee ,Attendance and Manager.

what is the mistake in my above code.

What I have tried:

< asp:Menu id="Menu1" runat="server" width="200px" >
< Items >
< asp:MenuItem Text="EMPLOYEE DETAILS" NavigateUrl="~/Employee_Details.aspx"
Value="Employee_Details" >
< /asp:MenuItem >

<asp:MenuItem Text="HR" NavigateUrl="~/HR.aspx" Value="HR">

<asp:MenuItem Text="Manager" NavigateUrl="~/manager.aspx" Value="Manager">





i want to hide the HR and Manager menu once user login show the Employee menu and remove the HR and Manager menu.

Note once user login it will redirect to Home page. home.aspx

in home.aspx all the menu name are visible.


i tried the below code once user login in to home page shows the Employee menu only and remove the HR and Manager menu.


In Login page i written the below code as follows

if (txtuser.Text == "admin")
{
var menu = Page.Master.FindControl("Menu1") as Menu;
if (menu != null)
{
menu.Items.Remove(menu.FindItem("Attendance"));
menu.Items.Remove(menu.FindItem("Manager"));
}
}

from the above code i am removing the Attendance and HR menu.

but when user login the home.aspx all the menus are displaying Employee ,Attendance and Manager.

what is the mistake in my above code.

推荐答案

当您删除代码中的菜单时,您只是为该实例删除它,即该页面请求。在下一页请求菜单都重新创建,因此您需要在每个请求上运行此代码,但如果您不想显示某些内容,将可见性设置为false则是传统的;



When you "remove" the menu in your code you are only removing it for that instance, ie that page request. On the next page request the menus are all re-created so you need to run this code on every request, though it is traditional to set the visibility to false if you don't want to show something;

menu.visible = false;





你需要这样做在每个页面请求您需要一种方法来记住用户是否登录。最简单的(但不是最好的,但我认为你只是在学习)是将它存储在Session中。所以当有人登录时会做类似的事情。





As you need to do this on every page request you need a way of remembering if the user is logged in or not. The simplest (but not best, however I assume you're just learning) is to store that in the Session. So when someone logs in do something like

Session["Username"] = txtuser.Text;





然后在页面加载事件中,您可以使用此会话变量来查看它们是否已登录,如果是,则根据该决定做出决定





Then on the page load event you can use this session variable to see if they are logged in and if so make decisions based on that

string username = null;
if (Session["Username"] != null)
{
    username = (string)Session["Username"];
}

if (username=="admin")
{
   menu.Visible = false;
}


这篇关于如何在ASP.NET中登录页面后删除菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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