For Answers, see/post comments

variable losing value on postback

I'm sure this is an easy question, but I need some help...

I have a variable, level, that starts out with a value of 1. After clicking a button, I need it to increase by one. Here's a code snippet:

private int _level;
public int Level
{
get
{
return _level;
}
set
{
_level = value;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Level = 1; //SET INITIAL VALUE OF LEVEL TO 1
}
}

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
Level += 1; //THE VALUE HERE IS RESET BACK TO 0
}

Any help you can provide is greatly appreciated.

Thank you,

9 comments:

Anonymous said...

Since you have no state, every time you post back to the page the variable is reset to 0 since it is a value type, and you don't have anything setting the variables value during a postback. So, the value becomes 0 again. You need to maintain state using something like session. See the code below:

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Level = 1;
Session.Add("MyCounter", Level);
}

else
{
Level = (int) Session.Contents["MyCounter"];
}

}

private void Button1_Click(object sender, System.EventArgs e)
{
Level++;
Session.Add("MyCounter", Level);
}

}

}

Hope this helps

DevSlick

Anonymous said...

If you are not planning to use that variable in any other page, then session might be an over kill. You could try using viewstate to store the variable on page load and read it from the viewstate on postback

Anonymous said...

I guess it just depends what he is doing..........I agree that state issues are rather specific to what you are trying to accomplish in the long run.

Anonymous said...

Agree DevSlick, I wasnt trying to belittle your approach. Was just suggesting him an alternative.

Take it easy

Anonymous said...

Also, you might try to declare your member variable as 'static':

private static int _level;

By doing so, your variable comes into existence before the execution of the static constructor for its containing type and is persisted in memory until its associated application domain ceases to exist, thereby maintaining its state throughout the app.

Hope this helps!

Var

Anonymous said...

Thanx Var!

The static constructor is the key! have seen this issue posted in a lot of forums, with lots of "interesting" solutions

Anonymous said...

Is there any solution other than making a static constructor? I am working in an ASP.NET application and need to pass the variable to different events within the same page. Usually button1 click pass the variable value to button 2 click handler, and button 2 click passes it to button 3 and so on. Is there a way without making it static, using session/viewstate/cache?

Anonymous said...

Use a hidden variable.. that can be passed between multiple postbacks..

I am not sure why you do not want to use Viewstate though.. if its one single variable that needs to persisted between multiple postbacks..

Anonymous said...

I've also got somewhat same kind of problem, and also had the static declaration as one of my options. But I think it'll be a disaster in multiuser environment cuz multiple users will be utilizing the same variable. So session or view state should be a safe option to ensure users' isolation. Plz do correct me If i'm wrong!


Thanks