Screenshot of webpage致力于创建一个一次显示一个问题的测试页面。其中四个单选按钮用于响应,两个按钮用于导航到下一个或上一个问题。当我点击上一个按钮时,响应清晰 如何保存单选按钮的响应。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class examination : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
DisplayQuestion();
}
public void DisplayQuestion()
{
// get data from session object
Examination e = (Examination)Session["questions"];
// display data
lblsubject.Text = e.sname;
lblQno.Text = e.curpos + 1 + "/" + e.SIZE;
lblCtime.Text = DateTime.Now.ToString();
lblStime.Text = e.StartTime.ToString();
Question q = e.question[e.curpos];
// display details of question
question.InnerHtml = q.question;
ans1.InnerHtml = q.ans1;
ans2.InnerHtml = q.ans2;
ans3.InnerHtml = q.ans3;
ans4.InnerHtml = q.ans4;
// reset all radio buttons
rbAns1.Checked = false;
rbAns2.Checked = false;
rbAns3.Checked = false;
rbAns4.Checked = false;
// disable and enable buttons
if (e.curpos == 0)
Button1.Enabled = false;
else
Button1.Enabled = true;
if (e.curpos == e.SIZE - 1)
Button2.Text = "Finish";
else
Button2.Text = "Next";
}
public void ProcessQuestion()
{
Examination exam = (Examination)Session["questions"];
Question q = exam.question[exam.curpos];
String answer;
// find out the answer and assign it to
if (rbAns1.Checked)
answer = "1";
else
if (rbAns2.Checked)
answer = "2";
else
if (rbAns3.Checked)
answer = "3";
else
if (rbAns4.Checked)
answer = "4";
else
answer = "0"; // error
q.answer = answer;
exam.question[exam.curpos] = q;
Session.Add("questions", exam);
}
protected void PreviousBtn_Click1(object sender, EventArgs e)
{
//ProcessQuestion();
Examination exam = (Examination)Session["questions"];
exam.curpos--;
Session.Add("questions", exam);
DisplayQuestion();
}
protected void NextBtn_Click(object sender, EventArgs e)
{
ProcessQuestion();
Examination exam = (Examination)Session["questions"];
if (exam.curpos == exam.SIZE - 1)
Response.Redirect("showresult.aspx");
else
{
exam.curpos++;
Session.Add("questions", exam);
DisplayQuestion();
}
}
protected void CancelBtn_Click(object sender, EventArgs e)
{
Examination exam = (Examination)Session["questions"];
Session.Remove("questions");
//exam = null;
Response.Redirect("default.aspx");
}
}
答案 0 :(得分:0)
ViewState是一个答案,但您需要完全理解使用ViewState的含义。打开ViewState会降低应用程序的速度,推送到视图状态的变量越多,页面加载速度就越慢。
状态持久性的每种机制都有它的优点,并且知道你正在做的事情的未来影响/限制是很重要的。
要了解有关在回发之间保持应用状态的更多信息,我建议您仔细阅读:
https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
具体回答您的问题:您的代码已经显示您正在使用SessionState。
因此,在您的DisplayQuestion()方法中,我将针对当前问题实施检查,从会话中检索并相应地更新复选框已检查状态。
注意:会话只能在单个服务器上运行,除非您使用状态服务器或SQL服务器来存储会话状态。