Total Field Problem

Discussion in 'ASP .Net' started by =?Utf-8?B?Sko=?=, Jan 10, 2005.

  1. 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?Sko=?=, Jan 10, 2005
    #1
    1. Advertising

  2. 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) {
    // Read from session
    RegTot=Convert.ToDouble(Session["RegTot"]);
    } else {
    // Store in session
    Session["RegTot"] = RegTot;
    }

    Anders Norås
    http://dotnetjunkies.com/weblog/anoras/
     
    =?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=, Jan 11, 2005
    #2
    1. Advertising

  3. 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 [MCAD]" wrote:

    > 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) {
    > // Read from session
    > RegTot=Convert.ToDouble(Session["RegTot"]);
    > } else {
    > // Store in session
    > Session["RegTot"] = RegTot;
    > }
    >
    > Anders Norås
    > http://dotnetjunkies.com/weblog/anoras/
    >
     
    =?Utf-8?B?Sko=?=, Jan 11, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Total Value for Field

    , Dec 4, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    378
  2. Ed

    Order 'total' problem.

    Ed, Dec 15, 2003, in forum: Javascript
    Replies:
    0
    Views:
    114
  3. FOX

    Form and total priece field

    FOX, May 20, 2006, in forum: Javascript
    Replies:
    0
    Views:
    99
  4. Sound
    Replies:
    2
    Views:
    498
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    492
Loading...

Share This Page