C# 如果(!IsPostback)没有';不存在于当前环境中

C# 如果(!IsPostback)没有';不存在于当前环境中,c#,webforms,C#,Webforms,我正在使用webform构建一个web应用程序,并尝试在会话中存储计数器,但我得到一个错误,即如果(!IsPostback)在当前上下文中不存在。有人能帮我解决这个问题吗 protected void Page_Load(object sender, EventArgs e) { if(!IsPostback) { Session["AttemptCount"] = 0; } } protected void submitAnswerButton_Click(object

我正在使用webform构建一个web应用程序,并尝试在会话中存储计数器,但我得到一个错误,即如果(!IsPostback)在当前上下文中不存在。有人能帮我解决这个问题吗

protected void Page_Load(object sender, EventArgs e)
{
        if(!IsPostback)
{
Session["AttemptCount"] = 0;
}

}

  protected void submitAnswerButton_Click(object sender, EventArgs e)
    {



        //difficultyList.SelectedIndex = 2;
        answerStatus.Visible = true;


        int answer = randomNumber1 + randomNumber2;
        int counter = (int)Session["AttemptCount"];
        if (mathAnswerTextBox.Text == answer.ToString())
        {
  counter++;
            Session["AttemptCount"] = counter;

            answerStatus.Text = "Correct!";


           // scoreLabel.Text = "Score: " + counter.ToString();
            randomNumber1 = random.Next(0, 50);
            randomNumber2 = random.Next(0, 50);
            num1Label.Text = Convert.ToString(randomNumber1);//add this line 
            num2Label.Text = Convert.ToString(randomNumber2); //and this line
            num1Label.Visible = true;
            num2Label.Visible = true;
            mathAnswerTextBox.Visible = true;

            submitAnswerButton.Visible = true;
        }
        else if (mathAnswerTextBox.Text != answer.ToString())
        {

            answerStatus.Text = "Incorrect";

            incorrectStrikes.Text = counter.ToString();
            num1Label.Visible = true;
            num2Label.Visible = true;
            mathAnswerTextBox.Visible = true;
            submitAnswerButton.Visible = true;
        //    scoreLabel.Text = counterArray[0].ToString();

        }

正如我之前在对你的问题的评论中提到的,你最初的问题是回发中的小写字母b。“AttemptCount”会话变量未递增的原因是if语句仅递增该变量:

if (mathAnswerTextBox.Text == answer.ToString())

我认为您希望在if语句的else子句中执行此操作,而不是在答案正确时执行此操作。

IsPostBack在示例代码中有一个lover case b。C#区分大小写,应该用大写字母B返回。谢谢。这确实奏效,但我还有一个问题。我的“计数器”不会再点击了。你知道为什么吗?我更新了上面的代码以向您显示该代码,该代码位于一个按钮内。@user3880824您需要在将计数器分配回会话之前,而不是分配回会话之后,增加计数器的数量。好的,我更改了代码,将计数器的数量增加到分配给会话的位置之上,但这仍然是一个问题??上面的代码也更新了。你知道IntelliSense吗?谢谢。这就是问题所在