Connecting Tech Pros Worldwide Forums | Help | Site Map

Preserving the value of an int between postbacks

Mike
Guest
 
Posts: n/a
#1: Nov 19 '05
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 adding
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 is
declared as a class variable. However when I post back the page the int gets
reset back to zero. Is there a way to preserve the value?

WJ
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Preserving the value of an int between postbacks


I would use Html Hidden field instead. On Page_load, do this:

//*********************
if(!IsPostBack)
{
htmlHiddenField.Value="0";
}

int hitCount=Convert.ToInt32(htmlHiddenField.Value); //current count
hitCount++; //Bump pass 1
htmlHiddenField.Value=hitCount.ToString(); //preserve it for next trip
//***********

John



"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
> adding
> 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
> is
> declared as a class variable. However when I post back the page the int
> gets
> reset back to zero. Is there a way to preserve the value?[/color]


David Young
Guest
 
Posts: n/a
#3: Nov 19 '05

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]


Mike
Guest
 
Posts: n/a
#4: Nov 19 '05

re: Preserving the value of an int between postbacks


Thanks for both suggestions it worked.

"WJ" wrote:
[color=blue]
> I would use Html Hidden field instead. On Page_load, do this:
>
> //*********************
> if(!IsPostBack)
> {
> htmlHiddenField.Value="0";
> }
>
> int hitCount=Convert.ToInt32(htmlHiddenField.Value); //current count
> hitCount++; //Bump pass 1
> htmlHiddenField.Value=hitCount.ToString(); //preserve it for next trip
> //***********
>
> John
>
>
>
> "Mike" <Mike@discussions.microsoft.com> wrote in message
> news:4CE4999B-252C-4EFB-AACD-F3A181D0613D@microsoft.com...[color=green]
> >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
> > adding
> > 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
> > is
> > declared as a class variable. However when I post back the page the int
> > gets
> > reset back to zero. Is there a way to preserve the value?[/color]
>
>
>[/color]
Closed Thread