62,046
社区成员
发帖
与我相关
我的任务
分享
由于字数的问题我只写了单选题
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);
}
}
}
}
}