我正在使用网络表单构建一个Web应用程序,并且我试图在会话中存储一个计数器,但我收到的错误是if(!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();
}
答案 0 :(得分:0)
正如我之前在对您的问题的评论中提到的,您的原始问题是在回发中使用小写 b 。你的“AttemptCount”会话变量没有递增的原因是因为你的if语句只增加它:
if (mathAnswerTextBox.Text == answer.ToString())
我想你想在你的if语句的else子句中这样做,而不是在答案是正确的时候。