Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a case where I'm in a class and accessing a varibal from a different class but I could only access the varible on page load of this class. Once the page load is complete the value in the variable is gone. But i need to store the varible's value permanatly so that even after the page load is complete I need to use the value to do some activity.

My question is how to store the session state varible's value on page load, so that I may be able to use the value eventhough the page load is finished.

Can I get a complete code if possible please? I'm new to it.
Posted
Updated 25-Nov-11 7:18am
v4
Comments
Dylan Morley 25-Nov-11 11:53am    
Moved content from other question:

rajh7: don't repost questions, just edit this question if you want to add extra details

You say page load so I'm assuming you're working in ASP.Net?

If it's just a small variable, such as a string or integer, consider storing the value in the users' session[^]. You can then retrieve the value from the session when you need to use it later
 
Share this answer
 
Comments
rajh7 25-Nov-11 12:10pm    
Yea you are right is asp.net. i acesed a button in my class but in page load . I need to have it saved so that i want to use the button once the page is loaded. So Can I use the similar code like the one you just showed me. Now I dont know how to retrieve the value back.? How do i code?
_Zorro_ 25-Nov-11 12:19pm    
You're only trying to validate your form? If yes, therer are other ways you should consider.
If I get the question correct, you want to set the value on 1st page load and then access it on button clikc in postback. Use the below in page_load():

C#
if(!IsPostBack)
   Session("myval")=value;


On button click, you can get the value you have set as below:

C#
string val = Session("myval").ToString();
 
Share this answer
 
Comments
rajh7 25-Nov-11 12:21pm    
Yea I checked and the button is already there when the page is loading. but once the page is loaded right ...what i mean is ok the page is up now i can see the page now right...now when i go check the button value in debugging mode its not there its null. here is my code

protected override void LoadToView(CaseStageDomObj caseStage)
{
if (Stage != null)
{
currentStageLit.Text = caseStage.StageLocale;
}

Button b = (Button)((ViewComplaint)Page).GetTheButtonFromPage();
button _button = b;
}
Nitesh Kejriwal 25-Nov-11 12:23pm    
please post your complete code to see what is the error.
rajh7 25-Nov-11 12:56pm    
protected override void LoadToView(CaseStageDomObj caseStage)
{
if (caseStage != null)
{
currentStageLit.Text = caseStage.StageLocale;
}


Button b = (Button)((ViewComplaint)Page).ReturnTheButton();
_button = b;


}

This is the page load code ok. when the page is being loaded the value of the button is disabled.i can check that with debug mode right. now after the page is loaded i want to set the button to enabled after some activities done in some other place . how do I do it? Just look at the button only please.
Nitesh Kejriwal 25-Nov-11 13:03pm    
After you have done the processing, do you have any reference to the button? I am not sure you have it..Also, let me know if the button is in same page or masterpage or any control? If you can use Javascript, you can do enable the button via jQuery too.
rajh7 25-Nov-11 13:18pm    
Yes you are right ..as soon as the page is loaded the button lost its state. it has null value. it had disabled value before by default. to my understanding the button is in the vew.aspx page. and i saw it in another place like protected global:: CNS.Web.Controls.Button transitionStageBtn; So I made a method in vew.aspx.cs( where button is available) that can return that button in user control class when i call it ok. now am at user control class and the method i made is returing the value successfully but only till the page is loading. after that its lot the value.
Hi,

Well, I'm not sure I understand.

What you describe seems impossible, could you tell us how are your classes structured?

How does the first class asks for the value stored in the other class? Is it a property?

Is it only the "variable" that gets "lost" or the class becomes null?

There are several ways to store data, (Cache, Session, Page items, Viewstate, etc.), but you shouldn't store what's not meant to. In this case, I think the problem is elsewhere.
 
Share this answer
 
Comments
rajh7 25-Nov-11 12:59pm    
this is my code
protected override void LoadToView(CaseStageDomObj caseStage) { if (caseStage != null) { currentStageLit.Text = caseStage.StageLocale; } Button b = (Button)((ViewComplaint)Page).ReturnTheButton(); _button = b; } This is the page load code ok. when the page is being loaded the value of the button is disabled.i can check that with debug mode right. now after the page is loaded i want to set the button to enabled after some activities done in some other place . how do I do it? Just look at the button only please.
rajh7 25-Nov-11 13:03pm    
Buttton is in the page and i accesed it in my user control class within the load method as i put it above. and i set the button to disable in the page already and when i check the button in my user control in page load its disable so good right...now i want to enable it after the page load is done...to do that i need to keep the state of the button right.
rajh7 25-Nov-11 14:19pm    
Thanks for your time
_Zorro_ 25-Nov-11 21:41pm    
Take a look at Nitesh Luharuka reply again, he's giving you the answer:

"After the page has loaded completely, you are no more on server rather on client..so, none of server code will work. To make them work use AJAX.."

It means either you do a postback or use client side code to do what you're looking to.
There are static variables.
ViewState
and Sessions.

static variables will be shared by all users. A global counter using Interlocked.Increment, for example.

ViewState and Sessions are usually seen as interchangeable, but they are not.
Imagine that the same user opens 2 windows/tabs of your site. Changing the value in session will affect the reload of both pages. ViewState, on the other side, is specific for each tab (for each "view" of your site).
 
Share this answer
 
 
Share this answer
 
Comments
_Zorro_ 25-Nov-11 12:06pm    
I may be wrong, and if it's the case I apologize but how does a static property could help? How would you ensure each user will have it's own value if it's a static property?
[no name] 25-Nov-11 12:19pm    
In your original question you didn't mention anything about multiple users or session state
_Zorro_ 25-Nov-11 12:25pm    
I'm not who asked the question. You're right, he didn't mention anything about that...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900