Asaf Pearl
Asaf Pearl

Reputation: 29

saving variables between page refresh

i need to do a basic web page (homework)

the user need to input number into textbox. after every number he need to press the "enter number" button.

and after a few numbers he need to press a different button to display his number and some calculations.

i cant use arrays ( homework limitations).

the problem is that after every time the user press "enter number" , the variables reset.

what can i do?

here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
       int totalSum=0;
       int mulOdd=1;
       int small=0;
       int big=0;
       float avg=0;
       int pairsum=0;
       int counter = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int t;
            counter++;

                t = Convert.ToInt16(TextBox1.Text);
                totalSum = totalSum + t;
                avg = totalSum / counter;
                int odd = t % 2;
                if (odd > 0)
                {
                    mulOdd = mulOdd * t;
                }
                else
                {
                    pairsum = pairsum + t;
                }

                if (t < small)
                {
                    small = t;
                }
                if (t > big)
                {
                    big = t;
                }

            TextBox1.Text = " ";`enter code here`
            Label1.Text = Convert.ToString(counter);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = "total Sum" + Convert.ToString(totalSum);
        }
    }
}

thank you.

Upvotes: 1

Views: 1075

Answers (5)

Icarus
Icarus

Reputation: 63966

Some pointers that you could extend to all your variables...

  1. Remove the global declaration of all of these variables, you don't need to have a global scope for them:

    int totalSum=0;
    int mulOdd=1;
    int small=0;
    int big=0;
    float avg=0;
    int pairsum=0;
    int counter = 0;
    
  2. On Button1_Click store the totalSum as so:

    ViewState["Sum"] = Convert.ToInt16(string.IsNullOrEmpty(ViewState["Sum"])?"0":ViewState["Sum"].ToString()  )+  Convert.ToInt16(TextBox1.Text);
    
  3. Finally, on Button_Click2 do:

    Label1.Text = "total Sum" + Convert.ToString(string.IsNullOrEmpty(ViewState["Sum"])?"0":ViewState["Sum"].ToString());
    

Upvotes: 0

Jim H.
Jim H.

Reputation: 5579

In your page load event, check the Page.IsPostback property. If true, then reset your variables based on the user input from the server controls HTML markup.

Read the links provided by @Oded. Page life cycle is a very important topic.

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37533

It's difficult to help without knowing the conditions of your assignment, but a method that would be usable would be to have a hidden field (webusercontrol) on the page that would hold a string. Each time the button is clicked, append the string representation of the number to the value of that control, making sure to separate the values with a symbol (comma, #, |, anything will do really). Then at any point during the process you would be able to get the value of that control, split it on the character, and compute whatever you needed to compute.

Since this is identified as homework, I won't be posting code, but this process should be pretty straight-forward.

Upvotes: 0

Daniel Casserly
Daniel Casserly

Reputation: 3500

You need to save the variables in state. There are several ways to pass variables

[1] Cookies (rarely found use for this)

[2] Session (Good for storing between pages for a different user)

[3] ViewState (Same page saving for a user)

[4] Query Strings (passing between pages)

Have a read about these and this should do the trick. Forgive me if i missed some out.

Upvotes: 2

Oded
Oded

Reputation: 499002

You need to read about the page life cycle and about state management, in particular view state.

Upvotes: 0

Related Questions