2013-12-15 32 views
-1

即时通讯设法使测验计划,但我的字符串阿尔法保存只有最后一个问题(如果question1被回答错了,它保存了它的错误,但如果question2是错误的,它只保存了问题2是错误回答覆写问题1是错误的回答。我想保存了错误的回答所有的问题不仅是最后一个,任何人都知道我在哪里出错在我的代码?字符串显示只有最后一个对象我添加(C#)

public partial class Exam : System.Web.UI.Page 
     { 
      ExamQuestionList questions; 
      int questionNumber; 
      string alfa; 
      public int Score 
      { 
       get { return (int)ViewState["score"]; } 
      } 

      protected void Page_Load(object sender, EventArgs e) 
      { 
       // initialise questionNumber and score 
       if (!IsPostBack) 
       { 
        ViewState["questionNumber"] = 0; 
        ViewState["score"] = 0; 

       } 

       questions = (ExamQuestionList)Application["questions"]; 

       // update questionNumber 
       questionNumber = (int)ViewState["questionNumber"] + 1; 
       ViewState["questionNumber"] = questionNumber; 

      } 

      protected void Page_PreRender(object sender, EventArgs e) 
      { 
       // display next question 
       Question q = questions[questionNumber - 1]; 
       lblQuestion.Text = "Question " + questionNumber + ": " + q.QuestionText; 
       rblOptions.DataSource = q.Options; 
       rblOptions.DataBind(); 
       rblOptions.SelectedIndex = 0; 
      } 

      protected void cmdNextQuestion_Click(object sender, EventArgs e) 
      { 
       // update score based on the answer to the previous question 
       int previousAnswer = rblOptions.SelectedIndex; 
       Question previousQuestion = questions[questionNumber - 2]; 
       if (previousAnswer == previousQuestion.CorrectAnswer) 
        ViewState["score"] = (int)ViewState["score"] + 1; 
       if (previousAnswer != previousQuestion.CorrectAnswer) 
        alfa =(questionNumber-1).ToString(); 
       if (alfa == null) 
       Label1.Text =""; 
       if (alfa != null) 
        Label1.Text = "you gave wrong answer to"+alfa; 
       // redirect to Result.aspx, if this is the last question 
       if (questionNumber == questions.Length) cmdNextQuestion.PostBackUrl = "~/Result.aspx"; 
      } 

     } 
+0

我在代码中没有看到任何类型的循环或列表。 – gunr2171

回答

4

我相信你保存该信息在变量“阿尔法”。问题是,阿尔法应该是一个列表或有东西分裂信息。

随着你有代码,y你永远不会得到你想要得到的结果。

alfa =(questionNumber-1).ToString(); 

你可以尝试什么是在列表中变换α和你的代码更改为类似(未测试)

string aux =(questionNumber-1).ToString(); 
alfa.Add(aux); 

编辑:刚才我注意到您正在使用ASP.NET和您合作需要在请求之间保持信息。有不同的选项可以做到这一点,您可以使用cookie,会话,控制状态,隐藏字段,查看状态,查询字符串和最终应用程序状态。基本上,您需要选择其中一个来保存当前列表并在下一个请求中从中读取它。

+0

我的问题编号是int,它不允许我将它添加到列表中;((奇怪,我认为.ToString()应该可以解决我的问题) – user2121038

+0

我将它转换为字符串,因为您可能有机会看到。可能只是有一个列表,你可以尝试更努力,你会得到你想要达到的解决方案,但是你不能放弃第一个问题 –

+0

我仍在努力,但我的列表只有一个元素,提示? – user2121038

相关问题