When are Session Variables Available in PostBacks?

  • Thread starter Phillip N Rounds
  • Start date
P

Phillip N Rounds

I'm writing a user control which has two states: Active & InActive. I
additionally am required that there to be only one active control per page,
and all logic has to be contained within the control.

In its inactive state, only a single button appears. If the user clicks on
this button, the control becomes active( the rest of the control's
functionality becomes visible), and all other instances of this user control
on the page should become inactive. When the user is finished with a
control, they click the 'Done' button, and the control becomes inactive.

I'm trying to do this by having a session variable for each control,
Session[ this.ControlID + "_ActiveState"] = { 0, 1 } and a single session
variable
Session[ "WhichControlIsActive "] = { 'None', "ControlID_1", "ControlID2",
.... , "ControlID_N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveState"] gets set to 1 and Session[ "WhichControlIsActive" ] =
this.ControlID; in the MyControl.ActivateButton_Click(..) method, and the
page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveState"] get set to 0, Session[
"WhichControlIsAcitve"] get set to 'None' in the
MyControl.ActivateButton_Click(..) method, and the page gets posted back.

I now want the new page to reflect the new session variables. My problem is
that the new session variables don't get saved until after the Page_Load
event of the new page, so these session variables are useless in structuring
the subsequent pages.

I can't use hidden fields, because they would have to be in a public place
( i.e. the containing page ), and I'm required to have all logic internal.
Similarly,
the Request.Form["_ "] isn't available to the controls.

Is there really no good way to do this?
 
M

Marina Levit [MVP]

It's not exactly clear to me what you are doing and what is going on.
However, session variables are there immediately. If you set a variable and
check its value in the next line of code, you will see it is there.

This sounds more of a timing issue, where you are trying to access the
session variables before they are set because you think some event is
happening first, and it isn't.
 
P

Phillip N Rounds

A simple example is as follows:

I created a WebForm with a single button, with code behind =

public class WebForm1 : System.Web.UI.Page
{ protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack) { Session["TestSession"] = "Defined by
Page_Load, !IsPostBack"; }

System.Diagnostics.Trace.WriteLine("From Page Load,
Session['TestSession' ] = " + Session["TestSession"].ToString() );
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Session["TestSession"] = "Button1_Click just changed
Session['TestSession']";
}
}

The following is the output


From Page Load, Session['TestSession' ] = Defined by Page_Load, !IsPostBack
From Page Load, Session['TestSession' ] = Defined by Page_Load, !IsPostBack
From Page Load, Session['TestSession' ] = Button1_Click just changed
Session['TestSession']

To get this, I had to click Button1 twice.
The first line is the inital page load,
The second line is after I clicked the Button. Note that the value of the
session variable hasn't changed, and
The third line is from the second click of the Button.

So, the effect of clicking the button is not apparent in the subsequent
page. I just want to be able to set the session variables in an event, and
have the results of these changes apparent in the subsequent page.


Phil


Marina Levit said:
It's not exactly clear to me what you are doing and what is going on.
However, session variables are there immediately. If you set a variable
and check its value in the next line of code, you will see it is there.

This sounds more of a timing issue, where you are trying to access the
session variables before they are set because you think some event is
happening first, and it isn't.

Phillip N Rounds said:
I'm writing a user control which has two states: Active & InActive. I
additionally am required that there to be only one active control per
page, and all logic has to be contained within the control.

In its inactive state, only a single button appears. If the user clicks
on this button, the control becomes active( the rest of the control's
functionality becomes visible), and all other instances of this user
control on the page should become inactive. When the user is finished
with a control, they click the 'Done' button, and the control becomes
inactive.

I'm trying to do this by having a session variable for each control,
Session[ this.ControlID + "_ActiveState"] = { 0, 1 } and a single session
variable
Session[ "WhichControlIsActive "] = { 'None', "ControlID_1",
"ControlID2", ... , "ControlID_N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveState"] gets set to 1 and Session[ "WhichControlIsActive" ] =
this.ControlID; in the MyControl.ActivateButton_Click(..) method, and
the page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveState"] get set to 0, Session[
"WhichControlIsAcitve"] get set to 'None' in the
MyControl.ActivateButton_Click(..) method, and the page gets posted back.

I now want the new page to reflect the new session variables. My problem
is that the new session variables don't get saved until after the
Page_Load event of the new page, so these session variables are useless
in structuring the subsequent pages.

I can't use hidden fields, because they would have to be in a public
place ( i.e. the containing page ), and I'm required to have all logic
internal. Similarly,
the Request.Form["_ "] isn't available to the controls.

Is there really no good way to do this?
 
M

Marina Levit [MVP]

This all looks correct to me.

Page_Load runs, prints it out

You click the button

Page_Load runs again, prints out what is already there

Then the Button Click handler runs, changes it.

You click again.

Page_Load runs again, prints out what the Button Click set.

This all looks exactly like what it is supposed to be.

Like I said, you have a misunderstanding of the timing of events. Page_Load
will run before any object event handlers do. Because the page has to load
everything up first.

Phillip N Rounds said:
A simple example is as follows:

I created a WebForm with a single button, with code behind =

public class WebForm1 : System.Web.UI.Page
{ protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack) { Session["TestSession"] = "Defined by
Page_Load, !IsPostBack"; }

System.Diagnostics.Trace.WriteLine("From Page Load,
Session['TestSession' ] = " + Session["TestSession"].ToString() );
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Session["TestSession"] = "Button1_Click just changed
Session['TestSession']";
}
}

The following is the output


From Page Load, Session['TestSession' ] = Defined by Page_Load,
!IsPostBack
From Page Load, Session['TestSession' ] = Defined by Page_Load,
!IsPostBack
From Page Load, Session['TestSession' ] = Button1_Click just changed
Session['TestSession']

To get this, I had to click Button1 twice.
The first line is the inital page load,
The second line is after I clicked the Button. Note that the value of the
session variable hasn't changed, and
The third line is from the second click of the Button.

So, the effect of clicking the button is not apparent in the subsequent
page. I just want to be able to set the session variables in an event,
and have the results of these changes apparent in the subsequent page.


Phil


Marina Levit said:
It's not exactly clear to me what you are doing and what is going on.
However, session variables are there immediately. If you set a variable
and check its value in the next line of code, you will see it is there.

This sounds more of a timing issue, where you are trying to access the
session variables before they are set because you think some event is
happening first, and it isn't.

Phillip N Rounds said:
I'm writing a user control which has two states: Active & InActive. I
additionally am required that there to be only one active control per
page, and all logic has to be contained within the control.

In its inactive state, only a single button appears. If the user clicks
on this button, the control becomes active( the rest of the control's
functionality becomes visible), and all other instances of this user
control on the page should become inactive. When the user is finished
with a control, they click the 'Done' button, and the control becomes
inactive.

I'm trying to do this by having a session variable for each control,
Session[ this.ControlID + "_ActiveState"] = { 0, 1 } and a single
session variable
Session[ "WhichControlIsActive "] = { 'None', "ControlID_1",
"ControlID2", ... , "ControlID_N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveState"] gets set to 1 and Session[ "WhichControlIsActive" ] =
this.ControlID; in the MyControl.ActivateButton_Click(..) method, and
the page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveState"] get set to 0, Session[
"WhichControlIsAcitve"] get set to 'None' in the
MyControl.ActivateButton_Click(..) method, and the page gets posted
back.

I now want the new page to reflect the new session variables. My
problem is that the new session variables don't get saved until after
the Page_Load event of the new page, so these session variables are
useless in structuring the subsequent pages.

I can't use hidden fields, because they would have to be in a public
place ( i.e. the containing page ), and I'm required to have all logic
internal. Similarly,
the Request.Form["_ "] isn't available to the controls.

Is there really no good way to do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top