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 :(得分:4)
我相信你正在保存关于变量“alfa”的信息。问题是alfa应该是一个List或者有什么东西可以拆分信息。
使用您拥有的代码,您将永远无法获得您想要获得的结果。
alfa =(questionNumber-1).ToString();
你可能会尝试在列表中转换alfa并将代码更改为(未经测试)
string aux =(questionNumber-1).ToString();
alfa.Add(aux);
编辑:我刚刚注意到您正在使用ASP.NET,您需要在请求之间保留信息。有不同的选项,你可以使用cookie,会话,控制状态,隐藏字段,查看状态,查询字符串,最后是应用程序状态。基本上,您需要选择其中一个来保存当前列表,并在下一个请求中从中读取它。