473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Preserving the value of an int between postbacks

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?
Nov 19 '05 #1
3 1421
WJ
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" <Mi**@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
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?

Nov 19 '05 #2
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" <Mi**@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
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?

Nov 19 '05 #3
Thanks for both suggestions it worked.

"WJ" wrote:
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" <Mi**@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
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?


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
2
by: Fred Newton | last post by:
Just wondering if there are any strong arguments one way or the other for storing a small value (6 bytes) in the ViewState vs. Session state. The value is needed in only one page in a large ASP.NET...
8
by: hb | last post by:
Hi, I need to declare a variable who's value can be preserve through the same ASP.Net page. I tried the following code, only the static variable s2 keeps its value=22 after lnk1_Click followed...
3
by: craigkenisston | last post by:
Hi, I am programming a classical registration form using asp.net. I have the country list and a list of all states for each country in a database. I already have this working code in which if...
0
by: jhonz | last post by:
I have come across a strange circumstance. I have user controls with dropdownlists that interact fine during times where the web server has minimal load. But once things pick up and postbacks...
0
by: Zoodor | last post by:
I have a server control that contains an HtmlSelect. It currently uses postbacks to function (and works fine), but I am upgrading it to be able to use callbacks (using postback or callback,...
1
by: satish.vell | last post by:
Hi, I have a ASP.NET page inside which I toggle the display of a field using javascript. How can I save this display value between postbacks. Here is my code: <pre>
1
by: Joe Abou Jaoude | last post by:
hi, I just want simply to preserve some data in my usercontrol on postbacks. so I added this property in my UserControl Protected Property MyProperty() As String Get If Not...
3
by: sophia.agnes | last post by:
Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.