Hallo Peter,
Yes, ASP.NET is by its nature, stateless. However, you can utilize state
with using Viewstate, Session state, Application State and caching. In your
case, what you need seems to be the Viewstate, which is a page level storage
rendered as a hidden field on the page. Whatever you write into Viewstate is
encrypted as base64 string and added to the HTML so you should be careful.
It's tamperable and it increases the page size.But for this case since you
put only a simple boolean variable, it's fine. Try the code below; I wrote it
on the fly so fill the gabs in.
private const string VIEWSTATE_DONE = "done";
private bool done = false;
private void Page_Load(...)
{
this.PreRender += new ....; //Set the PreRender event.
if(!this.IsPostBack)
{
done = (bool) this.ViewState[VIEWSTATE_DONE];
}
}
private void PreRender(....)
{
//our job with the variable is finished. Write it back to viewstate.
this.ViewState[VIEWSTATE_DONE] = done;
}
Don't forget to check out the alternative ways of storage I mentioned above.
Hope this helps,
Ethem
"Peter" wrote:
Hello!
I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it in
a function, like
if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}
But everytime when i use this function again after postback variable "done"
is set to "false" and WriteFile function is called again althought I set it
to true.
Is this because asp.net is stateless? Can I get around this using ViewState
to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.
Thanks in advance.
Peter