我现在用.NET2005做个毕业设计的网站(在线考试系统)我想在关闭浏览器的时候执行提交试卷的一个方法
方法如下:
protected void TiJiao()
{
string examresult = "", examresult1 = "";
int mark = 0, mark1 = 0;
CheckWhat("选择题");
mark = b;//分值赋给mark
CheckWhat("判断题");
mark1 = b;
float sum = 0, sum1 = 0;
for (int i = 0; i < StringAnswer.Length; i++)
{
try//异常判断
{
examresult = examresult + ItemCheck((Page.FindControl("List" + i) as RadioButtonList).SelectedIndex) + ",";//记录该考生做过的答案
if ((Page.FindControl("List" + i) as RadioButtonList).SelectedIndex == ABCDCheck(StringAnswer[i].Trim()))//判断该考生做过的答案是否是标准答案相同
{
sum = sum + mark;//相同则加相应的分值
}
}
catch
{
continue;
}
}//使考生状态表发生变化,以身改变相应的值
for (int j = 0; j < StringAnswer1.Length; j++)
{
try//异常判断
{
examresult1 = examresult1 + ItemTFCheck((Page.FindControl("buttonlist" + j) as RadioButtonList).SelectedIndex) + ",";//记录该考生做过的答案
if ((Page.FindControl("buttonlist" + j) as RadioButtonList).SelectedIndex == TFCheck(bool.Parse(StringAnswer1[j].Trim())))//判断该考生做过的答案是否是标准答案相同
{
sum1 = sum1 + mark1;//相同则加相应的分值
}
}
catch
{
continue;
}
}//
sum = sum + sum1;
sqlcon.Open();
sqlcom = new SqlCommand("update 考试状态表 set 分数=" + sum + " where 编号='" + Session["StudentID"].ToString().Trim() + "'", sqlcon);
sqlcom.ExecuteNonQuery();
if (checkb != 2)
{
sqlcom = new SqlCommand(" insert into 考生答案记录表 values(@id,@examid,@studentexam,@studentexam1,@biaozhi,@timediv)", sqlcon);//在数据库中记录该考生做的答案
}
else
{
sqlcom = new SqlCommand("update 考生答案记录表 set 试卷号=@examid,考生选择题答案=@studentexam,考生判断题答案=@studentexam1,标志=@biaozhi,还剩时间=@timediv where 考生编号=@id", sqlcon);
}
sqlcom.Parameters.Add("@id", SqlDbType.VarChar).Value = Session["StudentID"].ToString().Trim();
sqlcom.Parameters.Add("@examid", SqlDbType.VarChar).Value = count.ToString();
sqlcom.Parameters.Add("@studentexam", SqlDbType.Text).Value = examresult;
sqlcom.Parameters.Add("@studentexam1", SqlDbType.Text).Value = examresult1;
sqlcom.Parameters.Add("@biaozhi", SqlDbType.Bit).Value = 0;
sqlcom.Parameters.Add("@timediv", SqlDbType.Int).Value = 0;
sqlcom.ExecuteNonQuery();
Session["Mark"] = sum;//Session存放便于显示
Response.Redirect("Result.aspx");
sqlcon.Close();
}
进入到考试的代码是:
sqlcon.Open();
sqlcom = new SqlCommand("select *from ExamClassStudent where 学号
=@id and 姓名
=@name and 班级
=@class", sqlcon);
sqlcom.Parameters.Add("@id", SqlDbType.VarChar).Value = this.id.Text.Trim();
sqlcom.Parameters.Add("@name", SqlDbType.VarChar).Value = this.name.Text.Trim();
sqlcom.Parameters.Add("@class", SqlDbType.VarChar).Value = this.classid.SelectedValue.Trim();
reader = sqlcom.ExecuteReader();
if (reader.Read())
{
reader.Close();
Session["StudentID"] = this.id.Text.Trim();
Session["StudentClass"] = this.classid.SelectedValue.Trim();
Session["StudentName"] = this.name.Text.Trim();
Response.Redirect("StudentExam.aspx");
}
else
{
reader.Close();
Response.Write("<script>alert('考生信息错误!')</script>");
return;
}
我在考试的页面调用了一个JS是:
<script language="javascript">
window.onbeforeunload = function()
{
if((event.clientX>document.body.clientWidth&&event.clientY<0)||event.altKey) //关闭触发
{
<%TiJiao(); %>;
}
else
{
alert("刷新触发");
}
}
</script>
我点进入考试跳转到考试页面的时候,还没有考试就执行了提交方法!找不出哪里出问题了请老师您指点下!如果不用js的话是否可以有什么方法来实现关闭浏览器(考试页面)就能实现我的提交试卷的方法(TiJiao())?