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

Posted by MKS on Stack Overflow See other posts from Stack Overflow or by MKS
Published on 2011-01-16T08:26:01Z Indexed on 2011/01/16 11:53 UTC
Read the original article Hit count: 295

Filed under:
|
|

Hi Guys,

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET