Velocity Reviews > Total Field Problem

# Total Field Problem

=?Utf-8?B?Sko=?=
Guest
Posts: n/a

 01-10-2005
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

=?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=
Guest
Posts: n/a

 01-11-2005
JJ wrote:
> 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) {
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) {
RegTot=Convert.ToDouble(Session["RegTot"]);
} else {
// Store in session
Session["RegTot"] = RegTot;
}

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

=?Utf-8?B?Sko=?=
Guest
Posts: n/a

 01-11-2005
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?

JJ

> JJ wrote:
> > 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) {
> RegTot=Convert.ToDouble(Session["RegTot"]);
> } else {
> // Store in session
> Session["RegTot"] = RegTot;
> }
>
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/
>