页面生命周期中的值是否在变化?

时间:2012-05-11 14:32:24

标签: c# asp.net

我试图创建一个会生成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";
        }
    }

问题在于我注意到每次我尝试在文本框中回答问题时,数字都会改变。这就像我没有回答正确的问题。

2 个答案:

答案 0 :(得分:3)

问题是您使用的是静态变量。这些在所有请求中共享。不要为此目的使用静态变量,而是将它们存储在别处(例如,Session,ViewState,Hiddenfield,......)。

Nine Options for Managing Persistent User State in Your ASP.NET Application

  • 应用
  • 缓存
  • 表格发布/隐藏表格字段
  • 查询字符串
  • 会话
  • ASP.NET中的新状态容器
  • 高速缓存
  • 上下文
  • 视图状态
  • Web.config和Machine.config文件

......当然还有数据库。

答案 1 :(得分:3)

你似乎在回发时重新生成了一个新的数字对。基本上,逻辑应该是这样的,如果你计划反复停留在同一页面上并简单地发布答案:

1) Is this a postback?
   -> No: Generate a new value and store it in ViewState. Hold onto this value.
   -> Yes: Do we have a stored answer value in ViewState?
         -> No: Error? Something has gone wrong.
         -> Yes: Fetch that Value and hold on to it
2) On Button Click, Is the answer you entered the same as the generated value?
   -> No: Show Error
   -> Yes: Show Success, Then generate a new value and store it in ViewState

您的算法似乎只在回发期间和每次回发时生成新的答案对。