我先描述一下情况:首先这是一个在线考试的页面,页面的内容有:用户的姓名,考题(考题是从数据库中读取),还有一个提交按钮和一个计时的小textbox;在页面上我用客户端脚本实现了从0开始计时,当计时为90分钟时候我用想用if判断,跳转?执行服务器断的提交函数;我只有一个页面名称为usertest.aspx;提交函数在usertest.aspx.cs中,我应该怎样实现啊?
我先给出页面中计时的script脚本如下:
*****************************************************************************************************
    <script language="javascript" type="text/javascript">
<!--
        var ap_name = navigator.appName;
        var ap_vinfo = navigator.appVersion;
        var ap_ver = parseFloat(ap_vinfo.substring(0, ap_vinfo.indexOf('(')));
        var time_start = new Date();
        var clock_start = time_start.getTime();
        var dl_ok = false;
        
        
        var xmlhttp;  
          function init() {
            if (ap_name == "Netscape" && ap_ver >= 3.0)
                dl_ok = true;
            return true;
        }
        function get_time_spent() {
            var time_now = new Date();
            return ((time_now.getTime() - clock_start) / 1000);
        }
        function show_secs() // show the time user spent on the side
        {
            var i_total_secs = Math.round(get_time_spent());
            var i_secs_spent = i_total_secs % 60;
            var i_mins_spent = Math.round((i_total_secs - 30) / 60);
            var s_secs_spent = "" + ((i_secs_spent > 9) ? i_secs_spent : "0" + i_secs_spent);
            var s_mins_spent = "" + ((i_mins_spent > 9) ? i_mins_spent : "0" + i_mins_spent);
            document.form1.time_spent.value = s_mins_spent + ":" + s_secs_spent;
            window.setTimeout('show_secs()', 1000);
        }        window.onload = function() {
            test();
        }
        var sec = 0;
        var miu = 0;
        var hour = 0;
        var kj = 0;
        function test() {
            var time = document.getElementById("<%=timeBox.ClientID %>");
            if (time.value == "") {
                sec += 1;
                if (sec == 60) {
                    miu += 1;
                    sec = 0;
                }
                if (miu == 60) {
                    hour += 1;
                    miu = 0;
                }
                time.value = hour + ":" + miu + ":" + sec;
            } else {
                var ts = time.value.split(':');
                sec = parseInt(ts[2]);
                miu = parseInt(ts[1]);
                hour = parseInt(ts[0]);                sec += 1;
                if (sec == 60) {
                    miu += 1;
                    sec = 0;
                }
                if (miu == 60) {
                    hour += 1;
                    miu = 0;
                }
                time.value = hour + ":" + miu + ":" + sec;
            }            window.setTimeout("test()", 1000);
//            if(hour*60+miu>=5) {
//            <% 应该是在这里调用后台的函数吧,后台是newmethod()%>;}
        }
*******************************************************************************************
后台的提交按钮的click事件是条用提交函数,函数名称为NewMethod(); 
protected void imgBtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        NewMethod();
    }
***********************************************************************************************
newmethod()代码如下:
  public void NewMethod()
    {
        string Label = labSingle.Text;//单选分数
        string paperid = Session["PaperID"].ToString();
        string UserId = Session["userID"].ToString();
        DBHelp db = new DBHelp();
        foreach (RepeaterItem item in singleRep.Items)
        {
            HiddenField titleId = item.FindControl("titleId") as HiddenField;
            string id = (string)titleId.Value;
            string str = "";
            if (((RadioButton)item.FindControl("rbA")).Checked)
            {
                str = "A";
            }
            else if (((RadioButton)item.FindControl("rbB")).Checked)
            {
                str = "B";
            }
            else if (((RadioButton)item.FindControl("rbC")).Checked)
            {
                str = "C";
            }
            else if (((RadioButton)item.FindControl("rbD")).Checked)
            {
                str = "D";
            }
            string single = "insert into UserAnswer(UserID,PaperID,Type,TitleID,Mark,UserAnswer,ExamTime) values('" + UserId + "','" + paperid + "','单选题','" + id + "','" + Label + "','" + str + "','" + DateTime.Now.ToString() + "')";
            db.Insert(single);        }
        string labeM = Label3.Text;//多选分数
        foreach (RepeaterItem item in Repeater2.Items)
        {
            HiddenField titleId = item.FindControl("titleId") as HiddenField;
            string id = (string)titleId.Value;
            string str = "";
            if (((CheckBox)item.FindControl("CheckBox1")).Checked)
            {
                str += "A";
            }
            if (((CheckBox)item.FindControl("CheckBox2")).Checked)
            {
                str += "B";
            }
            if (((CheckBox)item.FindControl("CheckBox3")).Checked)
            {
                str += "C";
            }
            if (((CheckBox)item.FindControl("CheckBox4")).Checked)
            {
                str += "D";
            }
            string Multi = "insert into UserAnswer(UserID,PaperID,Type,TitleID,Mark,UserAnswer,ExamTime) values('" + UserId + "','" + paperid + "','多选题','" + id + "','" + labeM + "','" + str + "','" + DateTime.Now.ToString() + "')";
            db.Insert(Multi);
        }        string labeJ = Label4.Text;//判断分数
        foreach (RepeaterItem item in Repeater3.Items)
        {
            HiddenField titleId = item.FindControl("titleId") as HiddenField;
            string id = (string)titleId.Value;            string str = Convert.ToString(false);
            if (((RadioButton)item.FindControl("rbA")).Checked)
            {
                str = Convert.ToString(true);
            }
            else if (((RadioButton)item.FindControl("rbB")).Checked)
            {
                str = Convert.ToString(false);
            }
            string Judge = "insert into UserAnswer(UserID,PaperID,Type,TitleID,Mark,UserAnswer,ExamTime) values('" + UserId + "','" + paperid + "','判断题','" + id + "','" + labeJ + "','" + str + "','" + DateTime.Now.ToString() + "')";
            db.Insert(Judge);
        }        string labeF = Label5.Text;//填空分数
        foreach (RepeaterItem item in Repeater1.Items)
        {
            HiddenField titleId = item.FindControl("titleId") as HiddenField;
            string id = (string)titleId.Value;
            string str = "";
            str = ((TextBox)item.FindControl("TextBox1")).Text.Trim();
            string Fill = "insert into UserAnswer(UserID,PaperID,Type,TitleID,Mark,UserAnswer,ExamTime) values('" + UserId + "','" + paperid + "','填空题','" + id + "','" + labeF + "','" + str + "','" + DateTime.Now.ToString() + "')";
            db.Insert(Fill);        } string labeQ = Label6.Text;//问答分数
        foreach (RepeaterItem item in Repeater4.Items)
        {
            HiddenField titleId = item.FindControl("titleId") as HiddenField;
            string id = (string)titleId.Value;
            string str = "";
            str = ((TextBox)item.FindControl("TextBox2")).Text.Trim();
            string Que = "insert into UserAnswer(UserID,PaperID,Type,TitleID,Mark,UserAnswer,ExamTime) values('" + UserId + "','" + paperid + "','问答题','" + id + "','" + labeQ + "','" + str + "','" + DateTime.Now.ToString() + "')";
            db.Insert(Que);
        }
        // Session["Test"] = "eeee";        //Response.Write("<script language=javascript>alert('试卷提交成功!');window.close();</script>");
        Page.RegisterStartupScript(" ", " <script   language= 'javascript '> alert('试卷提交成功!');window.close(); </script> ");    }
*******************************************************************************************
出现的问题是,根本就不执行客户端中if判断,直接执行了newmethod方法;然后我做题后到了规定的时间不能交卷,除非点击一下“提交按钮”。而且显示时间的textbox也不显示时间了。
textbox不显示时间了。轻高手解答,或这给出新的实施方案。谢谢

解决方案 »

  1.   

    js 触发 提交事件(submit) 或 添加 asp:LinkButton 控件 使用 __doPostBack('','') 触发 LinkButton
    还可以使用js post 提交  var params = {};
      params.selType = selType;
      params.Content = document.getElementById().value;
      post('Evaluation.aspx', params);    function post(URL,PARAMS)
        {
            var from  = document.createElement("form");
            from.action = URL;
            from.method = "post";
            from.style.display = "none";
            for(var x in PARAMS)
            {
                var opt = document.createElement("textarea");
                opt.name = x;
                opt.value = PARAMS[x];
                from.appendChild(opt);
            }
            document.body.appendChild(from);
            from.submit();        
            window.close();
        }
      

  2.   

    了解一下jqury的ajax。不建议你这样写。