C# WinAppCalculator 到 Azure 中的 WebAppCalculator

C# WinAppCalculator to WebAppCalculator in Azure

您好,我对编码很陌生,尤其是在 C# 中,我的项目有问题。我正在寻找使用 c# 在线制作计算器并将其上传到数据库并以 Azure 形式托管。这是我的问题:

我在 Visual Studio 中使用 Web 表单时遇到 C# 代码问题。它根本不起作用,它可以输入数字和操作但是没有得到正确的结果,例如3 + 3 = 33。这是从 WinApp 转换过来的,所以它可能来自那里?但我重新创建了 UI 并重新调整了代码的用途以适应在线应用程序。在我开始工作后,我计划将其上传到 azure。有什么原因这不起作用吗?我在 .NET 中的 WinApp 有一个非常相似的代码并且可以工作,所以它是 .NET/ASP.net 问题吗?感谢您的帮助!

这是 .aspx.cs 文件:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : Page
    {
        bool operationPerf = false;
        string operation = "";
        double answer = 0;

        protected void NumbEvent(object sender, EventArgs e)
        {
            if (textbox.Text == "0" || operationPerf)
                textbox.Text = string.Empty;

            Button butt = (Button)sender;
            textbox.Text += butt.Text;
            operationPerf = false;

            label.Text = label.Text + " " + textbox.Text;
        }

        protected void OperandEvent(object sender, EventArgs e)
        {
            operationPerf = true;
            Button butt = (Button)sender;
            string newOperand = butt.Text;

            label.Text = label.Text + " " + newOperand;

            switch (operation)
            {
                case "+":
                    textbox.Text = (answer + Double.Parse(textbox.Text)).ToString();
                    break;
                case "-":
                    textbox.Text = (answer - Double.Parse(textbox.Text)).ToString();
                    break;
                case "*":
                    textbox.Text = (answer * Double.Parse(textbox.Text)).ToString();
                    break;
                case "/":
                    textbox.Text = (answer / Double.Parse(textbox.Text)).ToString();
                    break;
                case "^":
                    textbox.Text = (Math.Pow(answer, Double.Parse(textbox.Text))).ToString();
                    break;
                case "√":
                    textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
                    break;
                default:
                    break;
            }
            answer = Double.Parse(textbox.Text);
            operation = newOperand;
        }

        protected void Bequal_Click(object sender, EventArgs e)
        {
            operationPerf = true;

            switch (operation)
            {
                case "+":
                    textbox.Text = (answer + Double.Parse(textbox.Text)).ToString();
                    break;
                case "-":
                    textbox.Text = (answer - Double.Parse(textbox.Text)).ToString();
                    break;
                case "*":
                    textbox.Text = (answer * Double.Parse(textbox.Text)).ToString();
                    break;
                case "/":
                    textbox.Text = (answer / Double.Parse(textbox.Text)).ToString();
                    break;
                case "^":
                    textbox.Text = (Math.Pow(answer, Double.Parse(textbox.Text))).ToString();
                    break;
                case "√":
                    textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
                    break;
                default:
                    break;
            }
            label.Text = label.Text + " = " + textbox.Text;

            label.Text = "";
            answer = Double.Parse(textbox.Text);
            textbox.Text = answer.ToString();
            answer = 0;
            operation = "";
        }

        protected void BC_Click(object sender, EventArgs e)
        {
            textbox.Text = "0";
            label.Text = "";
            answer = 0;
            operation = "";
        }
    }
}

这是 .aspx 文件:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Calculator 9001</title>
</head>
<body>
    <form id="form1" runat="server">
        <p>
            <asp:Label ID="label" runat="server"></asp:Label>
        </p>
        <p>
            <asp:TextBox ID="textbox" runat="server" Height="35px" ReadOnly="True" Width="300px"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="Bdiv" runat="server" Height="75px" OnClick="OperandEvent" Text="/" Width="100px" />
            <asp:Button ID="Bmul" runat="server" Height="75px" OnClick="OperandEvent" Text="*" Width="100px" />
            <asp:Button ID="Bmin" runat="server" Height="75px" OnClick="OperandEvent" Text="-" Width="100px" />
            <asp:Button ID="Bplus" runat="server" Height="75px" OnClick="OperandEvent" Text="+" Width="101px" />
        </p>
        <p>
            <asp:Button ID="B7" runat="server" Height="75px" OnClick="NumbEvent" Text="7" Width="100px" />
            <asp:Button ID="B8" runat="server" Height="75px" OnClick="NumbEvent" Text="8" Width="100px" />
            <asp:Button ID="B9" runat="server" Height="75px" OnClick="NumbEvent" Text="9" Width="100px" />
            <asp:Button ID="Bpow" runat="server" Height="75px" OnClick="OperandEvent" Text="^" Width="100px" />
        </p>
        <p>
            <asp:Button ID="B4" runat="server" Height="75px" OnClick="NumbEvent" Text="4" Width="100px" />
            <asp:Button ID="B5" runat="server" Height="75px" OnClick="NumbEvent" Text="5" Width="100px" />
            <asp:Button ID="B6" runat="server" Height="75px" OnClick="NumbEvent" Text="6" Width="100px" />
            <asp:Button ID="Broot" runat="server" Height="75px" OnClick="OperandEvent" Text="√" Width="100px" />
        </p>
        <p>
            <asp:Button ID="B1" runat="server" Height="75px" OnClick="NumbEvent" Text="1" Width="100px" />
            <asp:Button ID="B2" runat="server" Height="75px" OnClick="NumbEvent" Text="2" Width="100px" />
            <asp:Button ID="B3" runat="server" Height="75px" OnClick="NumbEvent" Text="3" Width="100px" />
        </p>
        <p>
            <asp:Button ID="B0" runat="server" Height="75px" OnClick="NumbEvent" Text="0" Width="100px" />
            <asp:Button ID="Bdot" runat="server" Height="75px" OnClick="NumbEvent" Text="." Width="100px" />
            <asp:Button ID="BC" runat="server" Height="75px" OnClick="BC_Click" Text="C" Width="100px" />
            <asp:Button ID="Bequal" runat="server" Height="75px" OnClick="Bequal_Click" Text="=" Width="100px" />
        </p>
    </form>
</body>
</html>

更新:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["operationPerf"] = "false";
            ViewState["operation"] = string.Empty;
            ViewState["answer"] = "0";
        }
    protected void NumbEvent(object sender, EventArgs e)
    {
        if (textbox.Text == "0" || bool.Parse(ViewState["operationPerf"].ToString()) == true)
            textbox.Text = string.Empty;

        Button butt = (Button)sender;
        textbox.Text += butt.Text;
        ViewState["operationPerf"] = false;

        label.Text = label.Text + " " + textbox.Text;
    }

    protected void OperandEvent(object sender, EventArgs e)
    {
        ViewState["operationPerf"] = true;
        Button butt = (Button)sender;
        string newOperand = butt.Text;

        label.Text = label.Text + " " + newOperand;

        switch (ViewState["operation"])
        {
            case "+":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) + Double.Parse(textbox.Text)).ToString();
                break;
            case "-":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) - Double.Parse(textbox.Text)).ToString();
                break;
            case "*":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) * Double.Parse(textbox.Text)).ToString();
                break;
            case "/":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) / Double.Parse(textbox.Text)).ToString();
                break;
            case "^":
                textbox.Text = (Math.Pow(Double.Parse(Convert.ToString(ViewState["answer"])), Double.Parse(textbox.Text))).ToString();
                break;
            case "√":
                textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
                break;
            default:
                break;
        }
        ViewState["answer"] = textbox.Text;
        ViewState["operation"] = newOperand;
    }

    protected void Bequal_Click(object sender, EventArgs e)
    {
        ViewState["operationPerf"] = true;

        switch (ViewState["operation"])
        {
            case "+":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) + Double.Parse(textbox.Text)).ToString();
                break;
            case "-":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) - Double.Parse(textbox.Text)).ToString();
                break;
            case "*":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) * Double.Parse(textbox.Text)).ToString();
                break;
            case "/":
                textbox.Text = (Double.Parse(Convert.ToString(ViewState["answer"])) / Double.Parse(textbox.Text)).ToString();
                break;
            case "^":
                textbox.Text = (Math.Pow(Double.Parse(Convert.ToString(ViewState["answer"])), Double.Parse(textbox.Text))).ToString();
                break;
            case "√":
                textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
                break;
            default:
                break;
        }
        label.Text = label.Text + " = " + textbox.Text;

        label.Text = "";
        ViewState["answer"] = textbox.Text;
        textbox.Text = ViewState["answer"].ToString();
        ViewState["answer"] = 0;
        ViewState["operation"] = "";
    }

    protected void BC_Click(object sender, EventArgs e)
    {
        textbox.Text = "0";
        label.Text = "";
        ViewState["answer"] = 0;
        ViewState["operation"] = "";
    }
}

}

我认为是这些页面级变量。

bool operationPerf = false;
string operation = "";
double answer = 0;

就像@erastl 说的。这些变量不会保留它们的值。您需要页面上的隐藏字段或视图状态变量。

如果我没记错的话,ViewState 和 SessionState 变量始终是字符串,因此在使用它们时必须强制转换它们。 编辑:它们是对象。仍然需要转换。

protected void Page_Load(object sender, EventArgs e)
{
    ViewState["operationPerf"] = "false";
    ViewState["operation"] = string.Empty;
    ViewState["answer"] = "0";
}

稍后

if (textbox.Text == "0" || bool.Parse(ViewState["operationPerf"].ToString()) == true)
...

而不是

answer = Double.Parse(textbox.Text);

使用

ViewState["answer"] = textbox.Text;

当您实际使用该值进行计算时,请使用 Double.Parse