By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

When are Session Variables Available in PostBacks?

P: n/a
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?
Mar 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <pr*****@cassandragroup.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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?

Mar 2 '06 #2

P: n/a
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 [MVP]" <so*****@nospam.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
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" <pr*****@cassandragroup.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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?


Mar 2 '06 #3

P: n/a
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" <pr*****@cassandragroup.com> wrote in message
news:e2*************@TK2MSFTNGP11.phx.gbl...
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 [MVP]" <so*****@nospam.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
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" <pr*****@cassandragroup.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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?



Mar 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.