Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to store-read variable between post backs

Status
Not open for further replies.

alphajava

Programmer
Mar 30, 2005
17
US

I am trying to store an integer variable and make it persist in between post backs as follows:

private int imad;
// Inside Page_Load() I have this:
// Read val
if(this.ViewState["val"] == null)
{
val= 10;
}
else
{
val= (int)this.ViewState["val"] + 1;
}
// Store val
this.ViewState["val"] = val;

My problem is that at beginning of Page_Load()
ViewState["val"] is always = null. This means it is not persisting between post backs.

Please let me know if you have answer.
Thanks to all.
 
IsPostBack should not matter for what you are doing.
Are you posting back using a server side event or using Response.Redirect. If you are using Response.Redirect then you loose you viewstate.
Make sure you don't have EnableViewstate for your ASPX page set to false.
 

I think it is probably working! what confused me is that when I launch my page, IsPostBack is false, and it does not preserve state between page refreshes. Once I click on any control, which has the AutoPostBack property set to true, the IsPostBack becomes true and it works. Maybe this is how it works.

I am still wondering how do you set the "EnableViewstate" for the ASPX page?
I tried right mouse click and then property; I got the document property pages dialog, which does not have any setting for EnableViewstate.

Thanks to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top