ASP.NET考试系统考试提交后不跳转,如果正确就不显示,如果错误就显示正确答案在题目的旁边。怎么实现的!考试题目答错后,显示正确答案代码怎么写,写在什么地方?

fw2011_ren 2016-04-26 11:34:12
 由于字数的问题我只写了单选题

if (!IsPostBack)
{
if (Session["power"] == null)
{
Response.Write("<script>alert(\"对不起,您还未登录!\");</script>");
Response.Write("<script>window.location.href='announcement.aspx';</script>");
return;
}
if (Request.QueryString["ID"] == null)
{
Response.Redirect("announcement.aspx");
return;
}
Session["mainsrc"] = Request.Url.ToString();
}
if (Session["exam"] == null)
{
if (!readQuestions())
return;
}
createPaper();
}

protected void Button1_Click(object sender, EventArgs e)
{
if (Session["exam"] == null)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"当前会话已超时,请重新登录考试!\");</script>");
return;
}
DataSet ds = (DataSet)Session["exam"];
int score = 0;
for (int i = 0; i < ds.Tables["questions"].Rows.Count; i++)
{
if (ds.Tables["questions"].Rows[i]["题型"].ToString() == "单选题")
{
if (ds.Tables["questions"].Rows[i]["答案"].ToString() == ((RadioButtonList)table1.FindControl(ds.Tables["questions"].Rows[i]["ID"].ToString())).SelectedValue)
score += Convert.ToInt32(ds.Tables["exam"].Rows[0]["单选分"]);
}
if (ds.Tables["questions"].Rows[i]["题型"].ToString() == "多选题")
{
bool flag = false;
string temp = "";
for (int j = 0; j < ((CheckBoxList)table1.FindControl(ds.Tables["questions"].Rows[i]["ID"].ToString())).Items.Count; j++)
{
if (((CheckBoxList)table1.FindControl(ds.Tables["questions"].Rows[i]["ID"].ToString())).Items[j].Selected)
{
if (flag)
temp += "," + ((CheckBoxList)table1.FindControl(ds.Tables["questions"].Rows[i]["ID"].ToString())).Items[j].Value;
else
{
flag = true;
temp += ((CheckBoxList)table1.FindControl(ds.Tables["questions"].Rows[i]["ID"].ToString())).Items[j].Value;
}
}
}
if (temp == ds.Tables["questions"].Rows[i]["答案"].ToString())
score += Convert.ToInt32(ds.Tables["exam"].Rows[0]["多选分"]);
}
}
string sqlStr = "";

if (ds.Tables["exam"].Rows[0]["考试科目"].ToString() == "英语考试".ToString())
sqlStr = "insert into fxq_exam values(" + ds.Tables["exam"].Rows[0]["ID"].ToString() + "," + Session["ID"].ToString() + "," + score.ToString() + ")";
else if (ds.Tables["exam"].Rows[0]["考试科目"].ToString() == "数学考试")
sqlStr = "insert into hl_exam values(" + ds.Tables["exam"].Rows[0]["ID"].ToString() + "," + Session["ID"].ToString() + "," + score.ToString() + ")";
else
sqlStr = "insert into fg_exam values(" + ds.Tables["exam"].Rows[0]["ID"].ToString() + "," + Session["ID"].ToString() + "," + score.ToString() + ")";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"SORRY,录入考试成绩时出错!\");</script>");
return;
}
finally { conn.Close(); }
Session["exam"] = null;
Response.Redirect("oneself_fraction.aspx");

}


protected bool readQuestions()
{
DataSet ds = new DataSet();
string sqlStr = "select * from exam_tb where ID=" + Request.QueryString["ID"].ToString();
SqlDataAdapter ad = new SqlDataAdapter(sqlStr, conn);
try
{
conn.Open();
ad.Fill(ds, "exam");
conn.Close();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"读取考试信息出错!\");</script>");
return false;
}
finally { conn.Close(); }

if (ds.Tables["exam"].Rows[0]["考试科目"].ToString() == "英语考试")
sqlStr = "select * from fxq_exam where 考试号=" + Request.QueryString["ID"].ToString() + " and 证件号码=" + Session["ID"].ToString();
else if (ds.Tables["exam"].Rows[0]["考试科目"].ToString() == "数学考试")
sqlStr = "select * from hl_exam where 考试号=" + Request.QueryString["ID"].ToString() + " and 证件号码=" + Session["ID"].ToString();
else
sqlStr = "select * from fg_exam where 考试号=" + Request.QueryString["ID"].ToString() + " and 证件号码=" + Session["ID"].ToString();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
try
{
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
conn.Close();
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"您已进行过考试!\");document.location.href='announcement.aspx';</script>");
return false;
}
conn.Close();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"读取考试信息出错!\");</script>");
return false;
}
finally { conn.Close(); }

Label1.Text = ds.Tables["exam"].Rows[0]["ID"].ToString();
Label2.Text = ds.Tables["exam"].Rows[0]["考试科目"].ToString();
Label3.Text = ds.Tables["exam"].Rows[0]["考试名称"].ToString();
Label4.Text = Session["ID"].ToString();
Label5.Text = Session["uname"].ToString();
Label6.Text = (Convert.ToInt32(ds.Tables["exam"].Rows[0]["时间"]) * 60).ToString();

if (Convert.ToInt32(ds.Tables["exam"].Rows[0]["单选"]) > 0)
{
sqlStr = "select top " + ds.Tables["exam"].Rows[0]["单选"] + " * from questions_tb where 题型='单选题' order by newid()";
ad = new SqlDataAdapter(sqlStr, conn);
try
{
conn.Open();
ad.Fill(ds, "questions");
conn.Close();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "error", "<script>alert(\"读取题库出错!\");</script>");
return false;
}
finally { conn.Close(); }
}
Session["exam"] = ds;
return true;
}

protected void createPaper()//开始生成试卷页面
{
DataSet ds = (DataSet)Session["exam"];
DataRow[] single = ds.Tables["questions"].Select("题型='单选题'");
HtmlTableRow title;
HtmlTableCell cell;

if (single.Length > 0)
{
cell = new HtmlTableCell();
title = new HtmlTableRow();
cell.ColSpan = 2;
cell.Style.Add("font-weight", "bold");
cell.Style.Add("text-align", "left");
cell.InnerHtml = "单选题 (共 " + single.Length + " 题,每题 " + ds.Tables["exam"].Rows[0]["单选分"].ToString() + " 分)";
title.Cells.Add(cell);
table1.Rows.Add(title);
for (int i = 0; i < single.Length; i++)
{
HtmlTableRow row1 = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
cell1.Style.Add("width", "30px");
cell1.Style.Add("text-align", "center");
cell1.InnerHtml = (i + 1).ToString();
HtmlTableCell cell2 = new HtmlTableCell();
cell2.Style.Add("text-align", "left");
cell2.InnerHtml = single[i]["题目"].ToString();
row1.Cells.Add(cell1);
row1.Cells.Add(cell2);
HtmlTableRow row2 = new HtmlTableRow();
HtmlTableCell cell3 = new HtmlTableCell();
cell3.Style.Add("width", "30px");
cell3.InnerHtml = " ";
HtmlTableCell cell4 = new HtmlTableCell();
cell4.Style.Add("text-align", "left");
RadioButtonList list = new RadioButtonList();
list.ID = single[i]["ID"].ToString();
list.Width = Unit.Pixel(550);
ListItem l1 = new ListItem("A." + single[i]["A"].ToString(), "A");
ListItem l2 = new ListItem("B." + single[i]["B"].ToString(), "B");
ListItem l3 = new ListItem("C." + single[i]["C"].ToString(), "C");
ListItem l4 = new ListItem("D." + single[i]["D"].ToString(), "D");
list.Items.Add(l1);
list.Items.Add(l2);
list.Items.Add(l3);
list.Items.Add(l4);
cell4.Controls.Add(list);
row2.Cells.Add(cell3);
row2.Cells.Add(cell4);
table1.Rows.Add(row1);
table1.Rows.Add(row2);
}
}
}
}
}

...全文
190 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
a863922230 2016-04-29
  • 打赏
  • 举报
回复
label 就可以完全搞定了,很简单的。
plcly1 2016-04-29
  • 打赏
  • 举报
回复
label.visiable=false;
fw2011_ren 2016-04-28
  • 打赏
  • 举报
回复
隐蔽label怎么写啊?
fw2011_ren 2016-04-28
  • 打赏
  • 举报
回复
用不用保存试卷啊?隐藏label怎么着?
Nick狗蛋 2016-04-28
  • 打赏
  • 举报
回复
同意楼上说的。把正确答案放到一个Label里面,这个Label是处于隐藏的状态,当用户回答完问题点提交之后,就判断用户选择的答案与正确答案是否一致。如果一致的话加载下一题的内容,并把下一题的正确答案放到Label里面。如果答案不正确的话就提示错误,再把label显示出来。用户点击下一题之后再把label 隐藏,然后加载下一题的内容。
全栈极简 2016-04-27
  • 打赏
  • 举报
回复
不看你的代码,大概思路就是放个隐藏的Label之类的,有错误就把Label显示,Label存放正确答案。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧