Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7427843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:38:46+00:00 2026-05-29T08:38:46+00:00

I think my question revolves around me not having a comfortable grasp of page

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:38:47+00:00Added an answer on May 29, 2026 at 8:38 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think my question may be a duplicate, but I do not know what
I think the question is clear enough but still - what's the difference between:
I think this question is probably fairly simple, but I've been searching around and
I think this question has been asked, but I not found anything. From the
I think my question might seem a bit odd, but here it goes; I'm
I think this question has been asked many a times but I've been searching
I think the question title is clear enough: is is possible to stable_sort() a
I think the question is a bit silly, but for a newbie startup building
I think this question may end up being a bit subjective so I'm marking
I think the question is pretty simple, do I need all the rest of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.