473,739 Members | 6,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When are Session Variables Available in PostBacks?

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 + "_ActiveSta te"] = { 0, 1 } and a single session
variable
Session[ "WhichControlIs Active "] = { 'None', "ControlID_ 1", "ControlID2 ",
.... , "ControlID_ N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveSta te"] gets set to 1 and Session[ "WhichControlIs Active" ] =
this.ControlID; in the MyControl.Activ ateButton_Click (..) method, and the
page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveSta te"] get set to 0, Session[
"WhichControlIs Acitve"] get set to 'None' in the
MyControl.Activ ateButton_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
3 2678
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*****@cassan dragroup.com> wrote in message
news:O9******** ******@TK2MSFTN GP11.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 + "_ActiveSta te"] = { 0, 1 } and a single session
variable
Session[ "WhichControlIs Active "] = { 'None', "ControlID_ 1", "ControlID2 ",
... , "ControlID_ N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveSta te"] gets set to 1 and Session[ "WhichControlIs Active" ] =
this.ControlID; in the MyControl.Activ ateButton_Click (..) method, and the
page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveSta te"] get set to 0, Session[
"WhichControlIs Acitve"] get set to 'None' in the
MyControl.Activ ateButton_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
A simple example is as follows:

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

public class WebForm1 : System.Web.UI.P age
{ protected System.Web.UI.W ebControls.Butt on Button1;
private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !IsPostBack) { Session["TestSessio n"] = "Defined by
Page_Load, !IsPostBack"; }

System.Diagnost ics.Trace.Write Line("From Page Load,
Session['TestSession' ] = " + Session["TestSessio n"].ToString() );
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void Button1_Click(o bject sender, System.EventArg s e)
{
Session["TestSessio n"] = "Button1_Cl ick 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******** ******@TK2MSFTN GP12.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*****@cassan dragroup.com> wrote in message
news:O9******** ******@TK2MSFTN GP11.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 + "_ActiveSta te"] = { 0, 1 } and a single session
variable
Session[ "WhichControlIs Active "] = { 'None', "ControlID_ 1",
"ControlID2 ", ... , "ControlID_ N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveSta te"] gets set to 1 and Session[ "WhichControlIs Active" ] =
this.ControlID; in the MyControl.Activ ateButton_Click (..) method, and
the page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveSta te"] get set to 0, Session[
"WhichControlIs Acitve"] get set to 'None' in the
MyControl.Activ ateButton_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
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 misunderstandin g 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*****@cassan dragroup.com> wrote in message
news:e2******** *****@TK2MSFTNG P11.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.P age
{ protected System.Web.UI.W ebControls.Butt on Button1;
private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !IsPostBack) { Session["TestSessio n"] = "Defined by
Page_Load, !IsPostBack"; }

System.Diagnost ics.Trace.Write Line("From Page Load,
Session['TestSession' ] = " + Session["TestSessio n"].ToString() );
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void Button1_Click(o bject sender, System.EventArg s e)
{
Session["TestSessio n"] = "Button1_Cl ick 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******** ******@TK2MSFTN GP12.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*****@cassan dragroup.com> wrote in message
news:O9******** ******@TK2MSFTN GP11.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 + "_ActiveSta te"] = { 0, 1 } and a single
session variable
Session[ "WhichControlIs Active "] = { 'None', "ControlID_ 1",
"ControlID2 ", ... , "ControlID_ N" }

When the user selects an inactive control, its Session[ this.ControlID +
"_ActiveSta te"] gets set to 1 and Session[ "WhichControlIs Active" ] =
this.ControlID; in the MyControl.Activ ateButton_Click (..) method, and
the page gets posted back.

When the user is finished and clicks the 'Done' button, Session[
this.ControlID +"_ActiveSta te"] get set to 0, Session[
"WhichControlIs Acitve"] get set to 'None' in the
MyControl.Activ ateButton_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1760
by: baylor | last post by:
We have numerous pages that store datasets in Session. Sometimes these are used for one page, sometimes for a few (ex. - search page looks up your info, edit person page edits that info) What i'd like is a way to get rid of these session variables when we no longer need them. We originally trie putting cleanup code in Unload but then realized unload gets called after every page hit, including postbacks, so it wipes out data the active...
14
3235
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you go to is a login form, which will set several session variables with the name used to log in, appropriate security level and some other misc variables, and then will go to a main menu for each particular security level using Server.Transfer. ...
4
42041
by: Wayne | last post by:
Hi, I'm new to .NET and have a question about the use of static variables vs. session variables in a web form in C#. Instead of using a session variable to hold a string to persist during datagrid paging. Can I use a static variable? Also, if I use a static variable is that variable shared by everyone that brings up the aspx page? Ex, if 4 people are viewing the aspx page is an instance of the static variable created for each of the...
4
5592
by: PJ | last post by:
A particular page seems to be having issues with correctly setting Session variables. I am setting a couple of session variables on the Page_Unload event. While stepping through code, the immediate window will show the values in Session after the relevant lines that set the variables in the Page_Unload event. However, on postback, these variables are no longer in Session. All Session variables that were set previous to that particular...
4
4148
by: Ralph Krausse | last post by:
ViewState = "Bill"; -- This statement will send that to the browser and hash it into the __VIEWSTATE hidden varible Application = "Bill"; -- This statement will keep this info on the server Session = "Bill"; -- This statement will keep this info on the server
1
1769
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra properties declared on it over and above that provided by the textbox. As I understand it, part of the process to make this work is, in the property accessor's for the properties that you wish to persist you add
1
2097
by: Fabrício de Novaes Kucinskis | last post by:
Hi all, I have an ASP.net application in which I keep the user login in session variables. Today, with two users accessing simultaneously the application (in different sessions, but accessing the same page), I noticed that in some postbacks the returned page was not the one that the user was working; the data of the form are of the other user.
10
3512
by: tshad | last post by:
I have been using the default session state (InProc) and have found that I have been loosing my information after a period of time (normally 20 minutes). Is there anyway to find out how much more time I have on a session? If I do a refresh, does reset the session clock? Do you have have to go to another page to reset the session timeout or will a postback also do it? This is important as we have a few pages that a user
7
2240
by: Victor | last post by:
I've got two domain names sharing the same IP address that use ASP VBScript If I set a session variable with domain 1, it is only available for domain 1 - this is correct? If I set an application variable with domain 1, the app variable is sharing across all domains using that IP address - this is correct? This is the behavior I am seeing and I want to make sure that my server is set up correct. I especially want to make sure...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.