我試圖做一個簡單的網站,將產生2個數字,然後用戶將需要回答另外的這些數字2的正確的結果..價值在頁面生命週期中發生變化?
這是我的ASPX:
<div id="questionContainer">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="firstNumber">
<%=firstNum %>
</div>
<div id="operator">
<%=operatorString %>
</div>
<div id="secondNumber">
<%=secondNum %>
</div>
<div id="answerWrapper">
<input type="text" id="answer" name="answer" placeholder="your answer" />
<asp:Button runat="server" ID="submit" name="submit" OnClick="AnswerQuestion" Text="Answer" />
<input type="hidden" name="op" id="op" value="1" />
<div id="error">
<%=textForUser %>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
這是後臺代碼:
protected static int firstNum;
protected static int secondNum;
protected bool operatorTrueOrFalse = true;
protected string operatorString = "+";
protected GameLogic gl;
protected static int answer;
protected bool isItTrue;
protected int userAnswer;
protected string textForUser ="hello";
protected string op;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack || op == "1") // if solved X or null.
{
gl = new GameLogic();
firstNum = gl.GenerateNumber(4);
secondNum = gl.GenerateNumber(4);
answer = gl.SolveFor(firstNum, secondNum, operatorString);
}
}
public void AnswerQuestion(object sender, EventArgs e)
{
if (Request["answer"] != null)
{
userAnswer = int.Parse(Request["answer"]);
}
else
{
textForUser = "You need to type something";
}
if (answer == userAnswer)
{ // user has answered correctly.
isItTrue = true;
textForUser = "very good";
op = "1";
}
else
{// user has answered in - correctly.
isItTrue = false;
textForUser = "you are wrong";
op = "0";
}
}
的問題是,我注意到,每一次我嘗試回答在文本框中的問題,變化的數字。它就像我不能回答正確的問題。
+1一個不錯的路線圖。對讀者有幫助。 – Jeremy