我需要做一個基本的網頁(作業)頁面刷新之間保存變量
用戶需要輸入數字到文本框。在每個號碼之後,他需要按下「輸入號碼」按鈕。
和幾個數字後,他需要按不同的按鈕來顯示他的號碼和一些計算。
我不能使用數組(作業限制)。
問題是,每次用戶按「輸入號碼」後,變量都會重置。
我該怎麼辦?
下面的代碼:
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);
}
}
}
謝謝。
謝謝我會嘗試使用會話 –
如果它是相同的頁面的東西,我會考慮viewstate(在代碼中的工作方式幾乎相同) –