简体   繁体   中英

if(!IsPostback) doesn't exist in the current context

I'm building a web application using a webform and I'm trying to store a counter in a Session, but I'm getting the error that if(!IsPostback) doesn't exist in the current context. Can anyone help me with this issue?

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();

        }

As I previously mentioned in my comment to your question, your original problem was with the lowercase b in Postback. The reason your "AttemptCount" session variable is not being incremented is because your if statement only increments it:

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

I think you want to do this in the else clause of your if statement, and not when the answer is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM