如何解决类值初始化每次回发

本文关键字:初始化 何解决 解决 | 更新日期: 2023-09-27 18:02:09

我想写一个计算器作为一个网站,

我在类中设置了2个变量来存储数字,

但是每次我点击"+"或"-"按钮,

变量返回到初始语句

下面是我的代码:
public partial class _Default : System.Web.UI.Page
{
    int choice = 0;
    Boolean caluOrNot = false;
    double before, after;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Btn1_Click(object sender, EventArgs e)
    {
        if (this.caluOrNot == false)
        {
            if (txtResult.Text.ToString() == "0")
            {
                txtResult.Text = "1";
            }
            else
            {
                txtResult.Text += "1";
            }
        }
        else
        {
            txtResult.Text = "1";
        }
    }
    protected void Btn2_Click(object sender, EventArgs e)  
    protected void Btn3_Click(object sender, EventArgs e)
    protected void Btn4_Click(object sender, EventArgs e)
    protected void Btn5_Click(object sender, EventArgs e)
    protected void Btn6_Click(object sender, EventArgs e)
    protected void Btn7_Click(object sender, EventArgs e)    
    protected void Btn8_Click(object sender, EventArgs e)
    protected void Btn9_Click(object sender, EventArgs e)
    protected void Btn0_Click(object sender, EventArgs e)
    protected void BtnPoint_Click(object sender, EventArgs e)
    {
        txtResult.Text += ".";
    }
    protected void BtnJia_Click(object sender, EventArgs e)
    {
        this.choice = 1;
        this.caluOrNot = true;
        before = Double.Parse(txtResult.Text.ToString());
        txtCalu.Text = before.ToString() + "+";
    }
    protected void BtnJen_Click(object sender, EventArgs e)
    protected void BtnCheng_Click(object sender, EventArgs e)
    protected void BtnChu_Click(object sender, EventArgs e)
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtResult.Text = "0";
        this.choice = 0;
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        double a;
        switch (this.choice)
        {
            case 1:
                a = before + after;
                txtResult.Text = a.ToString();
                break;
            case 2:
                a = before - after;
                txtResult.Text = a.ToString();
                break;
            case 3:
                a = before * after;
                txtResult.Text = a.ToString();
                break;
            case 4:
                a = before / after;
                txtResult.Text = a.ToString();
                break;
            default:
                break;
        }
    }
}

作为代码,每次我点击btnJia将触发onclick方法BtnJia_Click

选择将设置为"1",caculOrNot设置为true,但BtnJia_Click结束时,值将变回0和false。

我怎么解决它?

如何解决类值初始化每次回发

当您单击BtnJia_Click时,一切都从头开始。因此,您需要在会话中存储caculOrNotchoice的值,并在需要时从会话中读取它们。下面是一个例子:

protected void BtnJia_Click(object sender, EventArgs e)
{
    this.choice = 1;
    this.caluOrNot = true;
    before = Double.Parse(txtResult.Text.ToString());
    txtCalu.Text = before.ToString() + "+";
    //Store them in Session
    Session["choice"] = this.choice;
    Session["caluOrNot"] = this.caluOrNot;
}
protected void btnGo_Click(object sender, EventArgs e)
{
    // Read them from Session when you need
    if(Session["choice"] != null)
    {
        this.choice = Convert.ToInt32(Session["choice"]);
    }
    double a;
    switch (this.choice)
    {
        case 1:
            a = before + after;
            txtResult.Text = a.ToString();
            break;
        case 2:
            a = before - after;
            txtResult.Text = a.ToString();
            break;
        case 3:
            a = before * after;
            txtResult.Text = a.ToString();
            break;
        case 4:
            a = before / after;
            txtResult.Text = a.ToString();
            break;
        default:
            break;
    }
    }
}