Last post Jul 15, 2011 08:08 AM by Alex69
Jul 12, 2011 05:57 AM|stefancelmicsisfant|LINK
Jul 12, 2011 06:00 AM|danyal316|LINK
make it a STATIC variable
Jul 12, 2011 06:07 AM|frez|LINK
make it a STATIC variable
Don't do this unless youi want the variable scope to be application rather than session wide.
ASP.NET is a stateless technology, everytime you access the server it starts afresh. If you want to 'remember' a value between trips to the server you will need to store it in either view state or session state.
Note, that is you store it in a static variable or in application state then it could be updated by any user session. That may or may not be what you desire in this case.
Jul 12, 2011 06:26 AM|Raigad|LINK
Hi use ViewState to store value
ViewState is the mechanism that allows state values to be preserved across page postbacks.
Jul 12, 2011 06:29 AM|stefancelmicsisfant|LINK
thank u for your answers. and let me make it more specific.
the variables i need to maintain are an HttpWebRequest, HttpWebResponse, a Stream that is continuously fed with input data from a server and an associated StreamReader. these need to be instantiated the first time the page is loaded.
the page contains a gridview that binds to its datasource every other n seconds, triggering a PostBack every time (this is when my variables lose their initial values).
for this setting, from what i read here http://msdn.microsoft.com/en-us/magazine/cc300437.aspx i think i need to store my variables in an Application object, because the variables are not
session specific (if two users access the page, they should see the same thing), can reach a great storage size and will be available the whole time the application runs. and apparently the user browser is not required to enable cookies.
please correct me if i am wrong.
Jul 12, 2011 06:33 AM|stefancelmicsisfant|LINK
yes, but deosn't ViewState store data for a user session?
Jul 12, 2011 06:35 AM|tjaank|LINK
the variables are not session specific (if two users access the page, they should see the same thing)
i think you need to use Cache for this
Jul 12, 2011 06:37 AM|tjaank|LINK
ViewState is specific for a Page ONLY.
NOT for User Session.
If user moves to next page, ViewState will be expired.
Where as, it is not recommended to use Session variables for huge data objects
However, in your case, you does not want to store User Specific data, then even Session variables will be useless
Application level Objects are initialized only ONCE, they retain their value until application is restarted.
Hence USELESS in this case
So i think CACHE would the thing you will need to use.
Jul 12, 2011 06:59 AM|stefancelmicsisfant|LINK
"Application level Objects are initialized only ONCE, they retain their value until application is restarted."
This is the behaviour i need. I need to initialise my variables once, when the application starts and maintain those values as long as the web page lives. As long as the connection with my server is opened, data will keep flowing into my Stream.
So why Cache and not Application?
(Thanks for clearifying ViewState, however i still wouldn'tuse it since it's page specific. )
Jul 12, 2011 07:07 AM|Bhavik Dharaiya|LINK
Jul 12, 2011 07:09 AM|Bhavik Dharaiya|LINK
Use hidden field to store the value.
Use label to store the value.
Use get set property to store the value.
Jul 12, 2011 07:29 AM|tjaank|LINK
data will keep flowing into my Stream
One thing to consider if you are using the Application object is that any writes to it should be done either in its Application_OnStart event (in global.asax) or within an Application.Lock section. While using Application.Lock is necessary to ensure that
writes are performed properly, it also serializes requests for the Application object, which can be a serious performance bottleneck for the application.
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Application["X"] = someValue;
protected void Button1_Click(object sender, EventArgs e)
int someNewValue = 123; // GET from HttpWebRequest/HttpWebResponse
Application["X"] = someNewValue;
Jul 12, 2011 08:54 AM|frez|LINK
The following article will help you understand the caching;
A combination of page level caching (so your page does not get recreated every time and so improves performance) and object caching may be appropriate in this case.
Jul 15, 2011 08:08 AM|Alex69|LINK
Or better construction is:
if(ViewState["x"] is int)
ViewState["x"] = value;
This way you can use x as an usual variable. But if you reload the page not via postback (via direct link clicking, or via Response.Redirect) then its value will be lost.