Login Session in same Master Page different pages

Baiti95 1 Reputation point
2022-03-28T06:24:32.407+00:00

I made a session in the master page and link with the web form. But it seems it only display the user name after login in home page only, when I move to other pages, the session didn't link. This is the code in the home page:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
Master.imgLogin.Visible = false;
Master.linklogout.Visible = true;
Master.labUser.Visible = true;
Master.labUser.Text = "Welcome " + Session["Username"];

            string conn = ConfigurationManager.ConnectionStrings["ConnHL"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(conn);
            string checkUser = "Select Name from tblUser where Username ='" + Session["Username"] + "'";
            SqlCommand comm = new SqlCommand(checkUser, sqlConn);
            sqlConn.Open();
            SqlDataReader sdr = comm.ExecuteReader();

            if(sdr.Read())
            {
                Session.Clear();
                Session.RemoveAll();
                string name = sdr["Name"].ToString();
                Master.labUser.Text = "Welcome " + name;

            }
            else
            {
                Response.Redirect("Logout.aspx");
            }


        }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,277 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 4,731 Reputation points
    2022-03-28T07:31:13.643+00:00

    Ok, the way this works?

    the master page load ONLY fires again WHEN you move to a different page - hence your comments about it only works when I move to a different page seems correct.

    However WHILE you on that current page? Then post-backs can freely occur in the child page, but the master page load event does not trigger or fire again.

    So, you need then to either mess around with code in the child page that changes the parent page.

    However, one really simple way - and since you don't have to do this all that often?

    Just in code navigate to the SAME page your currently on, and it will fire your master page load.

    So, say the page was say SignupInfo.aspx?

    Well, in your code - once you done your work - or whatever? then simple always navigate to where ever you want to go.

    However, if you going to remain on the same page? Then navigate to the same page.

    Response.Redirect("SignupInfo.aspx")

    So, if you jump to the same page you are on, then master page load event will trigger. Now, in some cases, this simple idea might not be what you want, but often, this "rare" need to re-fresh the master page (and probably see the menu bar update) can save you having to adopt messy code try and modify master page from child. Now, for your welcome message to work? You might have to shove it into session(), and then on page load check for that session() value, and set the label to show the welcome text/msg you have.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments