⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createtest.aspx.cs

📁 在线考试系统 主要真对考试 开发的个人项目
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class createTest : System.Web.UI.Page
{
    string courseId;
    string stuId;
    string stuName;
    string courseName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["stuId"] == null)
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            this.Button1.Attributes.Add("OnClick", "return confirm('你的确要交卷吗?交卷后将不能再考试!');");
            courseId = Convert.ToString(Session["courseId"]);
            stuId = Convert.ToString(Session["stuId"]);
            stuName = (new projClass()).getUserName(stuId);
            courseName = (new projClass()).getCourseNameAsCourseId(courseId);
            lbl_stuId.Text = stuId;
            lbl_stuName.Text = stuName;
            lbl_courseName.Text = courseName;
          
            //ifCreate表示是否已经生成过试题
            int ifCreate = (new projClass()).ifCourseHasTest(stuId, courseId);
            if (ifCreate != 1)           
            {
                DataBind1();
                SqlDataReader reader = (new projClass()).createTest(courseId);
                //一旦生成的试题就向分数表score里面添加记录,分数为0,并且在学生表student里面改学生的状态
                int count = (new projClass()).insertStuStatusToScore(stuId, courseId, 0, 0);
                int count1 = (new projClass()).updateStuStatus(stuId, courseId, 1);
                if (count == 0 || count1 == 0)
                    Response.Write("<script>alert(\"更新状态错误!\");</script>");
            }
        }

    }
    private void DataBind1() {
        DataSet ds1 = (new projClass()).getTestDanXuan(Session["courseId"].ToString()); 
        GridView1.DataSource = ds1;
        GridView1.DataBind();
        ((Label)GridView1.HeaderRow.FindControl("Label5")).Text = ((Label)GridView1.Rows[0].FindControl("Label31")).Text;


        DataSet ds2 = (new projClass()).getTestDuoXuan(Session["courseId"].ToString());
        GridView2.DataSource = ds2;
        GridView2.DataBind();
        ((Label)GridView2.HeaderRow.FindControl("Label10")).Text = ((Label)GridView2.Rows[0].FindControl("Label32")).Text;


        DataSet ds3 = (new projClass()).getTestIf(Session["courseId"].ToString()); 
        GridView3.DataSource = ds3;
        GridView3.DataBind();
        ((Label)GridView3.HeaderRow.FindControl("Label11")).Text = ((Label)GridView3.Rows[0].FindControl("Label33")).Text;


        DataSet ds4 = (new projClass()).getTianKong(Session["courseId"].ToString()); 
        GridView4.DataSource = ds4;
        GridView4.DataBind();
       ((Label)GridView4.HeaderRow.FindControl("Label12")).Text = ((Label)GridView4.Rows[0].FindControl("Label34")).Text;


        DataSet ds5 = (new projClass()).getWenDa(Session["courseId"].ToString()); 
        GridView5.DataSource = ds5;
        GridView5.DataBind();
       ((Label)GridView5.HeaderRow.FindControl("Label13")).Text = ((Label)GridView5.Rows[0].FindControl("Label35")).Text;
        
        
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Hashtable htsing = new Hashtable();
        Hashtable htMulit = new Hashtable();
        Hashtable htjudge = new Hashtable();
        Hashtable htfill = new Hashtable();
        Hashtable htquestions = new Hashtable();

        int score1 = 0;
        int size = 0;
        int singlemark = int.Parse(((Label)GridView1.Rows[0].FindControl("Label31")).Text);//取出单选题的每题分值
        foreach (GridViewRow dr in GridView1.Rows)//对单选题每题进行判断用户选择答案
        {

            string str = "";
            if (((RadioButton)dr.FindControl("RadioButton1")).Checked)
            {
                str = "1";

            }
            else if (((RadioButton)dr.FindControl("RadioButton2")).Checked)
            {
                str = "2";
            }
            else if (((RadioButton)dr.FindControl("RadioButton3")).Checked)
            {
                str = "3";
            }
            else if (((RadioButton)dr.FindControl("RadioButton4")).Checked)
            {
                str = "4";
            }
            if (((Label)dr.FindControl("Label4")).Text.Trim() == str)//将用户选择结果和答案进行比较
            {
                score1 = score1 + singlemark;
                size = size + 1;
            }
            htsing.Add(dr.RowIndex, str);

        }

        ///////////////////////////////////////////
        Session["htsing"] = htsing;//保存单选题答案
        ///////////////////////////////////////////

        int multimark = int.Parse(((Label)GridView2.Rows[0].FindControl("Label32")).Text);//取出多选题每题分值
        foreach (GridViewRow dr in GridView2.Rows)//对多选题每题进行判断用户选择答案
        {
            string str = "";
            if (((CheckBox)dr.FindControl("CheckBox1")).Checked)
            {
                str += "A";
            }
            if (((CheckBox)dr.FindControl("CheckBox3")).Checked)
            {
                str += "B";
            }
            if (((CheckBox)dr.FindControl("CheckBox2")).Checked)
            {
                str += "C";
            }
            if (((CheckBox)dr.FindControl("CheckBox4")).Checked)
            {
                str += "D";
            }
            if (((Label)dr.FindControl("Label16")).Text.Trim() == str)//将用户选择结果和答案进行比较
            {
                score1 = score1 + multimark;
                size = size + 1;
            }
            htMulit.Add(dr.RowIndex, str);

        }

        ///////////////////////////////////////////////
        Session["htMulit"] = htMulit;//保存多选题答案
        //////////////////////////////////////////////

        int judgemark = int.Parse(((Label)GridView3.Rows[0].FindControl("Label33")).Text);//取出判断题每题分值
        foreach (GridViewRow dr in GridView3.Rows)//对判断题每题进行判断用户选择答案
        {
            string j = "";
            if (((CheckBox)dr.FindControl("CheckBox5")).Checked)
            {
                j = "对";
            }           
            if (j == ((Label)dr.FindControl("Label23")).Text.Trim())
            {
                score1 = score1 + judgemark;
                size = size + 1;
            }
            htjudge.Add(dr.RowIndex, j);
        }

        ////////////////////////////////////////////
        Session["htjudge"] = htjudge;//保存判断题
        ///////////////////////////////////////////

        int fillmark = int.Parse(((Label)GridView4.Rows[0].FindControl("Label34")).Text);//取出填空题每题分值
        foreach (GridViewRow dr in GridView4.Rows)
        {
            string str = "";
            str = ((TextBox)dr.FindControl("TextBox1")).Text.Trim();
            if (str == ((Label)dr.FindControl("Label27")).Text.Trim())
            {
                score1 = score1 + fillmark;
                size = size + 1;
            }
            htfill.Add(dr.RowIndex, str);
        }

        ///////////////////////////////////////////////////////////
        Session["htfill"] = htfill;//保存填空题
        //////////////////////////////////////////////////////////
        
        string sc=((Label)GridView5.Rows[0].FindControl("Label35")).Text;
        int questionsmark = int.Parse(sc);//取出问答题每题分值
        foreach (GridViewRow dr in GridView5.Rows)
        {
           string testContent=((Label)dr.FindControl("Label29")).Text.Trim();
            string str = ((TextBox)dr.FindControl("TextBox2")).Text.Trim();
            string key = ((Label)dr.FindControl("Label26")).Text.Trim();
            string rightAns = ((Label)dr.FindControl("Label30")).Text.Trim();
            int marks = Marks(str, key, questionsmark);
            score1 = score1 + marks;
            size = size + 1;
            (new projClass()).insertTestWenDa(stuId,stuName,testContent,rightAns,key,str,courseName,sc,marks.ToString());
            htquestions.Add(dr.RowIndex, str);
        }

        ///////////////////////////////
        Session["htquestions"] = htquestions;//保存问答题
        ///////////////////////////////

        ///////////////////////统计分数
        //////////////////////////////////////////////////////////////////////////////////////////
        double allQuestion = GridView1.Rows.Count + GridView2.Rows.Count + GridView3.Rows.Count + GridView4.Rows.Count+GridView5.Rows.Count;
        double rate = (size / allQuestion) * 100;      
        Session["Mark"] = score1.ToString();
        string userid = Convert.ToString(Session["stuId"]);
        Page.RegisterStartupScript("555", "<script>windowopen();window.opener=null;window.close()</script>");
    }
    static int Marks(string str, string key, int score)
    {
        string fou = "not,不是,不,错,错误,不好,否,坏,不会,不对";
        string[] str1 = key.Split(',');
        string[] str2 = fou.Split(',');
        string[,] str3 = new string[str2.Length, str1.Length];
        string[] str4 = new string[str2.Length * str1.Length];
        int count = 0, count1 = 0;
        for (int i = 0; i < str1.Length; i++)
        {
            count += PanDuan(str, str1[i]);
        }

        try
        {
            for (int i = 0; i < str2.Length; i++)
            {
                for (int j = 0; j < str1.Length; j++)
                {
                    str3[i, j] = str2[i] + str1[j];
                }
            }
        }
        catch (Exception e)
        {
            //Console.WriteLine(e.Message); 
            throw new Exception(e.Message);
        }
        for (int i = 0; i < str2.Length; i++)
        {
            for (int j = 0; j < str1.Length; j++)
            {
                str4[i * str1.Length + j] = str3[i, j];
            }
        }

        for (int i = 0; i < str4.Length; i++)
        {
            count1 += PanDuan(str, str4[i]);
           
        }
        return (count - count1) * (score / str1.Length);
    }
    static int PanDuan(string str, string str1)
    {
        int count = 0;
        if (str.Contains(str1))
        {
            count++;
        }
        return count;
    }  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -