Page variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Peter,

Instance of the current page is created with every request and is destroyed
at the end of the response.
If you need to store the data for future reference you should consider using
ViewSatte
This is how you write to write state

if you variable was
bool done = false; at the start

you set it it
done = true;
on a certain event handling
then you use it to viewstate like this

ViewState["ActionComplete"] = this.done;

you can read it in page load using something like

if(ViewState["ActionComplete"] != null)
{
this.done = (bool)ViewState["ActionComplete"];
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
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
 
Hi,

This is the common way of storing a variable in the viewstate:

public SomeType PropertyName
{
get
{
object a = ViewState["PropertyName"];
if(a != null)
return (SomeType)a;
return some_default_value; // or null when possible
}
set{ViewState["PropertyName"] = value;}
}

The property should be member of the page class, of course.

Hope this helps
Martin
 
Thanks very much for your replies guys :) I think the ViewState is most
useful for my issue, so I try to use it.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top