Search code examples
c#asp.net.netsession-variablespage-lifecycle

simple login page and dynamically hiding controls based on session variable


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:

    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";
    }

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!


Solution

  • 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.

    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;
        }
    }