Total Field Problem

G

Guest

Hi,

I have a textbox setup on a web form and it suppose to keep a running
total of some other textboxs on page. In code behind I have a global double
var that is suppose to collect the total but doesn't work.

Here's what it looks like:

In global area I have:
protected double RegTot = 0.0;


In Method I have:
RegTot += RegTot + TextBox1.value;

when ever I change TextBox1.value, RegTot is always zero. So I only get the
present value in textbox1 and never any previous values that should be in
RegTot. How should I code this so I can collect the values in Textbox1
multiple times and store in RegTot for a final amount? This is not in a table
or datagrid.

Thanks,

JJ
 
G

Guest

JJ said:
Here's what it looks like:

In global area I have:
protected double RegTot = 0.0;


In Method I have:
RegTot += RegTot + TextBox1.value;

when ever I change TextBox1.value, RegTot is always zero. (abriged)
ASP.NET doesn't maintain the object state between postbacks. To preserve
the value of RegTot you'll have to store it in the view state or in a
session variable.

To store the value in and retrieve it from the view state do this:
if (Page.IsPostBack) {
// Read from view state
RegTot=Convert.ToDouble(ViewState["RegTot"]);
} else {
// Store in view state
ViewState["RegTot"] = RegTot;
}

To store the value in and retrieve it from session do this:
if (Page.IsPostBack) {
// Read from session
RegTot=Convert.ToDouble(Session["RegTot"]);
} else {
// Store in session
Session["RegTot"] = RegTot;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
G

Guest

Hi Anders,

I was wondering also I had built a separate class to store the Running
Total for page but It would do the same thing and return zero for previous
value entered.
So the class doesn't persist for the life of the page I assume?

Thanks for replying,

JJ


Anders Norås said:
JJ said:
Here's what it looks like:

In global area I have:
protected double RegTot = 0.0;


In Method I have:
RegTot += RegTot + TextBox1.value;

when ever I change TextBox1.value, RegTot is always zero. (abriged)
ASP.NET doesn't maintain the object state between postbacks. To preserve
the value of RegTot you'll have to store it in the view state or in a
session variable.

To store the value in and retrieve it from the view state do this:
if (Page.IsPostBack) {
// Read from view state
RegTot=Convert.ToDouble(ViewState["RegTot"]);
} else {
// Store in view state
ViewState["RegTot"] = RegTot;
}

To store the value in and retrieve it from session do this:
if (Page.IsPostBack) {
// Read from session
RegTot=Convert.ToDouble(Session["RegTot"]);
} else {
// Store in session
Session["RegTot"] = RegTot;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top