| re: Preserving the value of an int between postbacks
Mike,
Since HTTP is a stateless protocol, you have to find a way to persist the
value between posts. There are several methods to do this.
1) Use hidden fields: You could use a hidden field to hold the value of the
int you are wanting to increment.
Example: int myInt = Convert.ToInt32(myhiddenfield.Text); Then you can
increment as needed.
2) Use the session object: You can save any object to session.
Example:
Session["myInt"] = 1;
int myInt = Convert.ToInt32(Session["myInt"]);
There are other means as well, but those two are the most common.
"Mike" <Mike@discussions.microsoft.com> wrote in message
news:4CE4999B-252C-4EFB-AACD-F3A181D0613D@microsoft.com...[color=blue]
> I am developing an ASP.NET app in C#. I am attempting to use an int as a
> counter which will be incremented each time a user submits a webform[/color]
adding[color=blue]
> data to a list. Initially I tried to use a session variable, but had some
> issues converting that value to an int. So I've tried using an int which[/color]
is[color=blue]
> declared as a class variable. However when I post back the page the int[/color]
gets[color=blue]
> reset back to zero. Is there a way to preserve the value?[/color] |