如何在我的C#程序中保存单选按钮的响应
继续创建一个测试页面,每次显示一个问题。其中四个单选按钮用于回答,两个按钮用于导航到下一个或上一个问题。当我点击“上一步”按钮时,响应是清晰的 如何保存单选按钮的响应如何在我的C#程序中保存单选按钮的响应,c#,asp.net,C#,Asp.net,继续创建一个测试页面,每次显示一个问题。其中四个单选按钮用于回答,两个按钮用于导航到下一个或上一个问题。当我点击“上一步”按钮时,响应是清晰的 如何保存单选按钮的响应 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;
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");
}
}
ViewState是一个答案,但您需要完全理解使用ViewState的含义。打开ViewState会降低应用程序的速度,并且您将更多变量推入ViewState,页面加载速度就会降低 每种状态持久性机制都有其优点和缺点。了解您所做工作的未来影响/限制非常重要 要了解有关在回发之间保持应用程序状态的更多信息,我建议您通读以下内容: 具体回答您的问题:您的代码已经显示您正在使用SessionState 因此,在DisplayQuestion()方法中,我将对从会话中检索到的当前问题进行检查,并相应地更新复选框的选中状态