简单的登录页面,基于会话变量的动态隐藏控件 [英] simple login page and dynamically hiding controls based on session variable

查看:138
本文介绍了简单的登录页面,基于会话变量的动态隐藏控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的问题都是围绕着我不要有一个舒适的把握页面生命周期中的ASP.net还是不幸。香港专业教育学院读了很多,但它的很多采取,对不起!反正我尝试做一个超级简单页面的概念是什么,我会在整个网站上做一个证明,所以先我只是发表我有:

I think my question revolves around me not having a comfortable grasp of page life cycle in ASP.net still unfortunately. Ive read a lot but its a lot to take in, sorry! anyways im trying to make a super simple page as a proof of concept for what I will do across the entire site so first I will just post what I have:

ASPX:

<asp:Button ID="btnLogin" runat="server" Text="Login" 
onclick="btnLogin_Click" />

<hr />

<asp:Label ID="Label1" runat="server" Text="Regular User"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server">Regular User</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Regular User" />

<hr />

<asp:Label ID="Label2" runat="server" Text="Admin"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server">Admin</asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Admin" />

ASPX.CS:

ASPX.CS:

    protected void Page_Load(object sender, EventArgs e)
    {

        String admin = (String)(Session["admin"]) ?? "";

        if (!admin.Equals("true"))
        {
            Label2.Visible = false;
            TextBox2.Visible = false;
            Button2.Visible = false;
        }

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Session["admin"] = "true";
    }

所以,我希望所有的随机控制隐藏,一旦用户变为管理员又名会话变量的变化。我的问题是的后,按一下按钮的控件不会隐藏自己的直接。我还需要刷新或导航离开和回来。经处理页面生命周期的问题之前,我认为这是什么搞乱了起来,也许我只是需要把知名度禁用比的Page_Load其他功能?反正有可能是100%更好,更简单的方法,我愿意接受这样刚刚还是让我知道这样做完全 - !谢谢

So I want all the random controls to hide once the user "becomes an admin" aka the session variable change. My problem is the controls won't hide themselves directly after the button click. I have to refresh or navigate away and come back. Having dealt with page life cycle issues before I think that is what's messing it up, perhaps I just need to put the visibility disables in a function other than Page_Load? Anyways there could be a 100% better and simpler way to do this altogether which I am open to so just lemme know - thanks!

推荐答案

Page.Load后Button.Click处理程序发生。这意味着在单击该按钮时,您需要在您为了使更改出现在第一页上渲染后,按一下按钮设置会话变量设置控件的状态。

Button.Click handlers occur after Page.Load. This means that when the button is clicked, you need to set your controls' states after you set the session variable in order for the changes to appear on the first page render after the button click.

此外,ViewState的将preserve你上的控件设置的属性,所以你需要检查这两个登录和退出条件,并设置适当的状态,在两个方向。

Also, ViewState will preserve the properties you set on the controls, so you need to check for both logged in and logged out condition and set the proper state in both directions.

code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["admin"] = null;
    }
    Set_Control_State();
}

protected void btnLogin_Click(object sender, EventArgs e)
{
    String admin = (String)(Session["admin"]) ?? "";
    if (admin.Equals("true"))
    {
        Session["admin"] = null;
    }
    else
    {
        Session["admin"] = "true";
    }
    Set_Control_State();
}

protected void Set_Control_State()
{
    String admin = (String)(Session["admin"]) ?? "";

    if (admin.Equals("true"))
    {
        btnLogin.Text = "Log Out";
        Label2.Visible = true;
        TextBox2.Visible = true;
        Button2.Visible = true;
    }
    else
    {
        btnLogin.Text = "Log In";
        Label2.Visible = false;
        TextBox2.Visible = false;
        Button2.Visible = false;
    }
}

这篇关于简单的登录页面,基于会话变量的动态隐藏控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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