Adding/removing session variables on Page OnInit/OnLoad in C#

15,041

Solution 1

OK i think i get it. In your first method that is OnInit you are only checking for boolSignOn to be null or empty but not for the panelOpen.

try this intead:

 if (Session["boolSignOn"].ToString() == "true".ToString() && Session["panelOpen"] != null)   

Also change your else part. and change your whole code to:

Update here is the full code i tried

 protected override void OnInit(EventArgs e)
{
    try
    {
        //Change your condition here
        if (Session["boolSignOn"].ToString() == "true".ToString() && Session["panelOpen"] != null)               
        {              
            lblPanelOpen.Text = Session["panelOpen"].ToString();
        }
        else
        {
            //Dont set text to panelOpen here
            lblPanelOpen.Text = string.Empty;
        }
    }
    catch (Exception ex)
    {
        Logger.Error("Error processing request:" + ex.Message);
    }
}

protected override void OnLoad(EventArgs e)
{
    try
    {
        if (!string.IsNullOrEmpty(Session["panelOpen"].ToString()))
        {
            // No need to set it here it will be set in next load in OnInit call 
            //lblPanelOpen.Text = string.Empty;
            Session.Remove("panelOpen");
        }
   }
   catch (Exception ex)
   {
       Logger.Error("Unable to remove the session variable:" + ex.Message);
   }

}

I hope this may work. Give it a try

Solution 2

Sorry I am not able to understand your problem correctly however I can see a blue print of your issue and here are few steps that you may want to follow to see if this fixes your issue.

1) When the user opens a panel (assuming there should be a click event when the user clicks to open the panel). So add code in the click event to create and save a session variable with the Panel ID that is open.

2) Assuming that the user has completed the login process when you redirect the user back to the same page you should be able to access the session variable and also remove it in OnLoad event.

3) If this does not work try using Server.Transfer to redirect the user back to the same page.

4) Also did you tried doing Session.Clear(); does this work?

Solution 3

Ok here we go,

When the user press the login link or button you store a value in a session variable in your case

Session["boolSignOn"] = true;
Session["panelOpen"] = blah blah; // your thing to keep;

The user login and the page redirected to the home page in the page load you check the Session["boolSignOn"] if it has value and it is true then you apply the panel changes you need.

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["boolSignOn"] != null && (bool)Session["boolSignOn"] == true)
    {
       // get the Session["panelOpen"] and do the changes you need.
    }
}

In your home page Page_Unload if the Session["boolSignOn"] has value and it is true you clear both Session["boolSignOn"] and Session["openPanel"],

protected void Page_Unload(object sender, EventArgs e)
{
    if (Session["boolSignOn"] != null && (bool)Session["boolSignOn"] == true)
    {
        Session["boolSignOn"] = null;
        Session["openPanel"] = null;
    }
}

I hope this is what you are looking for.

Share:
15,041
Manoj Singh
Author by

Manoj Singh

http://www.linkedin.com/profile/view?id=4359501&trk=hb_tab_pro_top

Updated on June 14, 2022

Comments

  • Manoj Singh
    Manoj Singh almost 2 years

    I am using C#.

    I am having below code in C#:

    protected override void OnInit(EventArgs e)
    {
        try
        {
            if (Session["boolSignOn"].ToString() == "true".ToString())              
            {              
                lblPanelOpen.Text = Session["panelOpen"].ToString();
            }
            else
            {
                lblPanelOpen.Text = Session["panelOpen"].ToString();
            }
        }
        catch (Exception ex)
        {
            Logger.Error("Error processing request:" + ex.Message);
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(Session["panelOpen"].ToString()))
            {
                lblPanelOpen.Text = string.Empty;
                Session.Remove("panelOpen");
            }
        }
        catch (Exception ex)
        {
            Logger.Error("Unable to remove the session variable:" + ex.Message);
        }
    }
    

    In above code I am having a Session["panelOpen"] variable which is created from another user control and once my page is trying to render, I am storing Session["panelOpen"] in my hidden lblPanelOpen.Text on page OnInit() method, however when page is loaded completely then I am trying to remove the session variable.

    Please suggest!