简体   繁体   English

会话验证母版页(?)asp.net c#

[英]session verification masterpage (?) asp.net c#

I have a blank page with two buttons. 我有两个按钮的空白页。

the first button's click code is this: 第一个按钮的点击代码是这样的:

Session["permissionUser"] = "1";

and here's the second button code: 这是第二个按钮代码:

Session["permissionUser"] = "2";

and then i have a hyperlink which redirects to the "main" website. 然后我有一个超链接,它会重定向到“主要”网站。

my objective is to adapt the menu bar which is on the masterpage based on the permission saved in the session. 我的目标是根据会话中保存的权限调整母版页上的菜单栏。 here's part of my code in the masterpage: 这是我在母版页中的代码的一部分:

<body>
<div id="menuBar">
<a href="../Default.aspx">Home</a>
<% if (Session["permissionUser"] == "1"){ %>
<a href="#">PERMISSION 1 LINK</a>
<% } %>
<% if (Session["permissionUser"] == "2"){ %>
<a href="#">PERMISSION 2 LINK</a>
<% } %>
</div>

<div id="content">
<asp:ContentPlaceHolder ID="websiteContent" runat="server"></asp:ContentPlaceHolder>
</div>
</body>

the problem is when i run the application, even if i click any of the buttons the menu doesnt adapt at all. 问题是当我运行该应用程序时,即使我单击任何按钮,菜单也完全不适应。 it just shows the hyperlink "Home" and not any of the others which were supposed to be shown since the session is either 1 or 2 (depending on which button i clicked) 它仅显示超链接“主页”,而不显示任何其他应显示的超链接,因为会话为1或2(取决于我单击的按钮)

i cant really see what im doing wrong so if you guys have any suggestions i'd be really grateful 我真的看不到我在做什么错,所以如果你们有什么建议,我将非常感激

Your code is very PHPish. 您的代码非常PHPish。 That is to say, it's ugly. 也就是说,这很丑。 And unwieldy. 而且笨拙。 Let's put the logic in the code behind. 让我们将逻辑放在代码后面。 We also need a form so we can have controls that run on the server. 我们还需要一个表单,以便我们可以在服务器上运行控件。

public void Page_Load(object sender, EventArgs e)
{
    //you should probably also check to make sure the session has "permissionUser" in it
    if (Session["permissionUser"] == "1")
    {
        Permission1HL.Visible=true;
    }
    else if(Session["permissionUser"] == "2")
    {
        Permission2HL.Visible=true;
    }
}

And change your ASPX page to this. 并将您的ASPX页面更改为此。

<body>
<form runat="server">
<div id="menuBar">
<a href="../Default.aspx">Home</a>
<asp:HyperLink runat="server" id="Permission1HL" Text="Permission 1 Link" Visible="false" />
<asp:HyperLink runat="server" id="Permission2HL" Text="Permission 2 Link" Visible="false" />
</div>

<div id="content">
<asp:ContentPlaceHolder ID="websiteContent" runat="server"></asp:ContentPlaceHolder>
</div>
</form>
</body>

I would actually be more specific in the if statement 我实际上在if语句中会更具体

<% if (Session["permissionUser"].toString() == "1"){ %>

with null checks 空检查

<% if (Session["permissionUser"] != null && Session["permissionUser"].toString() == "1"){ %>

I suggest that you make a serverside hyperlink control instead, and set the text and navigateurl from codebehind 我建议您改用服务器端超链接控件,并从代码隐藏中设置文本和Navigationurl

<asp:HyperLink id="hyperlink1" 
              NavigateUrl="http://mydefaulturl.com"
              Text="DefaultText"
              runat="server"/>

from code behind: 从后面的代码:

if (Session["permissionUser"] == 1)
{
  hyperlink1.NavigateUrl = "#"
  hyperlink1.Text = "Permission 1 link"
}...

This will allow you to better control and debug your values. 这将使您能够更好地控制和调试值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM