0

私はウェブサイトとしてcaculatorを書こうとしています、

クラスに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に設定されますが、終了時に値は0に戻り、falseに戻りますBtnJia_Click

どうすれば解決できますか?

4

1 に答える 1

1

ASP.NET では、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;
    }

    }
}

セッション状態の詳細については、こちらをご覧ください。

于 2013-03-05T06:59:24.340 に答える