[英]Using function in ASP.NET web application
不好意思,我是C#的初學者,在創建計算器Web應用程序時需要幫助和一些指導。 因此,我被賦予了使用ASP.NET Web應用程序Web表單和UI來創建計算器Web應用程序的任務,如下所示:
關鍵是,我犯了一個錯誤,而是改用Windows Forms App(WFA),可以使計算器正常工作。
但是,當我嘗試使用ASP.NET Web應用程序Web表單制作計算器時,計算器將無法工作,因為以某種方式設置為運行該方法的變量沒有任何值,這與在WFA中運行該變量不同。
這是我的代碼:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool lastpressedIsOperation;
string input = String.Empty;
protected void num_Click(object sender, EventArgs e)
{
Button button = sender as Button;
//this.display.Text = "";
input+=button.Text;
if (display.Text == "0" && display.Text != null)
{
display.Text = button.Text;
}
else
{
display.Text += button.Text;
}
}
protected void op_Click(object sender, EventArgs e)
{
Button button = sender as Button;
input+=button.Text;
if (display.Text == "0" && button.Text == "-" && display.Text != null)
{
display.Text = button.Text;
}
else
{
display.Text += button.Text;
}
}
protected void del_Click(object sender, EventArgs e)
{
//this.display.Text = input.ToString(0,input.Length);
}
protected void del_all_Click(object sender, EventArgs e)
{
this.display.Text = "0";
this.input = string.Empty;
}
private void enter_Click(object sender, EventArgs e)
{
string inFix = input;
string isValid = Validate(inFix);
string rpn = ConvertToPostFix(isValid);
string result = Convert.ToString(CalculateRPN(rpn));
this.display.Text = result;
input = result;
}
private static string Validate(string inFix)
{
StringBuilder newstring = new StringBuilder(inFix);
Stack<int> lb_index = new Stack<int>();//stack for left bracket index
Queue<int> rb_index = new Queue<int>();//stack for right bracket index
char temp = '#';
Console.WriteLine("temp: ", temp);
for (int i = 0; i < newstring.Length; i++)
{
if (newstring[i] == '(')
lb_index.Push(i);
else if (newstring[i] == ')')
rb_index.Enqueue(i);
Console.WriteLine("temp: {0}", temp);
if (newstring[i] == '-')//change unary - to ~
{
if (temp.IsOperator())
{
newstring[i] = '~';
}
}
temp = newstring[i];
}
if (lb_index.Count == rb_index.Count)
{
bool bracket_valid = true;
for (int i = 0; i < lb_index.Count; i++)
{
if (lb_index.Pop() > rb_index.Dequeue())
{
bracket_valid = false;
break;
}
}
if (bracket_valid != true)
{
newstring.Clear();
newstring.Append("Error, Bracket wrong");
}
}
else if (lb_index.Count < rb_index.Count || lb_index.Count > rb_index.Count)
{
newstring.Clear();
newstring.Append("Error, Bracket wrong");
}
Console.WriteLine("newstring = {0}", newstring);
return newstring.ToString();
}
我的想法是,我想在用戶使用數字和操作按鈕輸入值並按下Enter鍵之后,從文本框中獲取字符串。
然后首先使用Validate(inFix)
驗證字符串,然后將其格式化為后綴ConvertToPostFix(isValid)
,然后使用CalculateRPN(rpn)
計算得出。
但我不知道為什么isValid
變量從不從Validate(inFix)
獲取值,這會導致其他方法無法正常工作。 在ASP Web應用程序表單中如何使用該功能有什么區別? 如果是這樣,我該如何使用方法/功能?
還有沒有更好的方法來實現這一點,以便我可以完成任務?
我相信您的問題是您正在設置:
string inFix = input;
在你的
enter_Click
方法。
然而,
input
變量初始化為:
string input = String.Empty;
因此,每次加載表單時,即在初始加載或回發時,輸入變量都會重新初始化為空字符串。
我不會在這里重新創建帖子,但是要解決此問題,您需要執行以下操作:
Session["INPUT"] = input;
只要修改了輸入,就執行以下操作:input =(string)Session [“ INPUT”]; 在頁面加載時初始化輸入變量。 如果是初始頁面加載,則輸入變量將為null(即,會話變量Session [“ INPUT”]不存在)。
基本上,我的意思是,盡管asp.net會話在窗體上存儲控件的狀態,但它不會保存添加到頁面上的自己的類變量的狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.