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

📄 createtest.aspx.cs.bak

📁 考试系统,测试帐号:学生ID:012003021314
💻 BAK
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace TestOnline
{
	/// <summary>
	/// createTest 的摘要说明。
	/// </summary>
	public class createTest : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lbl_stuId;
		protected System.Web.UI.WebControls.Label lbl_stuName;
		protected System.Web.UI.WebControls.Label lbl_courseName;
		protected System.Web.UI.WebControls.ImageButton Ibtn_submit;
		protected System.Web.UI.WebControls.ImageButton Ibtn_createTest;
		protected System.Web.UI.WebControls.Panel Panel2;
		protected System.Web.UI.WebControls.LinkButton LinkButton2;
		protected System.Web.UI.WebControls.Panel Panel1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(Session["stuId"]==null)
			{
					Response.Redirect("login.aspx");
			}else
			{
			Ibtn_submit.Attributes.Add("OnClick","return confirm('你的确要交卷吗?交卷后将不能再考试!');");
			string courseId=Convert.ToString(Session["courseId"]);
			string stuId=Convert.ToString(Session["stuId"]);
			string stuName=(new projClass()).getUserName(stuId);
			string courseName=(new projClass()).getCourseNameAsCourseId(courseId);
			lbl_stuId.Text=stuId;
			lbl_stuName.Text=stuName;
			lbl_courseName.Text=courseName;
				/*
				以下生成试题的思路:
				 通过存储过程在试题库里随机的选择出50道题,一旦生成后即往分数表里面添加记录,分数为0.
				 试题生成后,将试题的ID都存入一个哈希表,如果用户刷新,则从哈希表里取出试题的ID,再次生成,从而达到了防刷新的目的.(传统的考试系统在刷新之后能
				 再次生成试题)
				 
				*/

			//ifCreate表示是否已经生成过试题
			int ifCreate=(new projClass()).ifCourseHasTest(stuId,courseId);
				if(ifCreate!=1)
					//if(!Page.IsPostBack)
				{
					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>");
					Hashtable ht_rightAnswer=new Hashtable();
					Hashtable ht_stuTest=new Hashtable();//存储学生第一次进入页面时产生的题目ID,以免刷新时题目改变.
					int num=1;
					while(reader.Read())
					{
						Literal lit_testContent=new Literal();
						RadioButtonList rbtnList=new RadioButtonList();
						rbtnList.ID="rbtnList"+num.ToString();
						//rbtnList.RepeatDirection=RepeatDirection.Horizontal;
						lit_testContent.Text="<br>"+num.ToString()+"、"+Server.HtmlEncode(reader["testContent"].ToString())+"<br>";
						rbtnList.Items.Add("A. "+Server.HtmlEncode(reader["testAns1"].ToString()));
						rbtnList.Items.Add("B. "+Server.HtmlEncode(reader["testAns2"].ToString()));
						rbtnList.Items.Add("C. "+Server.HtmlEncode(reader["testAns3"].ToString()));
						rbtnList.Items.Add("D. "+Server.HtmlEncode(reader["testAns4"].ToString()));
						//得到题目的正确答案,并添加到哈希表中
						string testId=reader["testId"].ToString();
						ht_stuTest.Add(num.ToString(),testId.ToString());
						int rightAns=(new projClass()).getRightAnsAsTestId(testId);
						ht_rightAnswer.Add(num.ToString(),rightAns);
						//	Response.Write(rightAns.ToString());
						for(int j=1;j<=4;j++)
						{
							rbtnList.Items[j-1].Value=j.ToString();
						}
						if(num<=25)
						{
							Panel1.Controls.Add(lit_testContent);
							Panel1.Controls.Add(rbtnList);
						}
						else
						{
							Panel2.Controls.Add(lit_testContent);
							Panel2.Controls.Add(rbtnList);
						}
						num++;
					}
					Session["rightAnswer"]=ht_rightAnswer;
					Session["stuTest"]=ht_stuTest;
				}
			
					//不是第一次进入页面
				else	
				{    //   int num=1;
					//if(Session["ht_stuTest"]==null)
					//Response.Write("<script>alert(\"由于刷新产生了错误!请重新登录或联系管理员!\");</script>"); 
				
					Hashtable ht_stuTest=(Hashtable)Session["stuTest"];
					string testId="";
			
					DataSet ds;
					bool find;
					for(int num=1;num<=50;num++)
					{
							find=false;
						IDictionaryEnumerator myEnumerator = ht_stuTest.GetEnumerator();
						while(myEnumerator.MoveNext()&&!find)
        
							if(myEnumerator.Key.Equals(num.ToString()))
							{
									testId=myEnumerator.Value.ToString();
								find=true;
								ds=(new projClass()).getTestInfoAsId(testId);
								DataTable dt=ds.Tables["testInfo"];
								DataRow dataRow=dt.Rows[0];
								Literal lit_testContent=new Literal();
								RadioButtonList rbtnList=new RadioButtonList();
								rbtnList.ID="rbtnList"+num.ToString();
								lit_testContent.Text="<br>"+num.ToString()+"、"+Server.HtmlEncode(Convert.ToString(dataRow["testContent"]))+"<br>";
								rbtnList.Items.Add("A. "+Server.HtmlEncode(Convert.ToString(dataRow["testAns1"])));
								rbtnList.Items.Add("B. "+Server.HtmlEncode(Convert.ToString(dataRow["testAns2"])));
								rbtnList.Items.Add("C. "+Server.HtmlEncode(Convert.ToString(dataRow["testAns3"])));
								rbtnList.Items.Add("D. "+Server.HtmlEncode(Convert.ToString(dataRow["testAns4"])));
								//得到题目的正确答案,并添加到哈希表中
						
								//ht_stuTest.Add(num.ToString(),testId.ToString());
								int rightAns=(new projClass()).getRightAnsAsTestId(testId);
								for(int j=1;j<=4;j++)
								{
									rbtnList.Items[j-1].Value=j.ToString();
								}
								if(num<=25)
								{
									Panel1.Controls.Add(lit_testContent);
									Panel1.Controls.Add(rbtnList);
								}
								else
								{
									Panel2.Controls.Add(lit_testContent);
									Panel2.Controls.Add(rbtnList);
								}
							}
					}
				
			
				}
				}

		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Ibtn_submit.Click += new System.Web.UI.ImageClickEventHandler(this.Ibtn_submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Ibtn_submit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			Hashtable ht_stuAnswer=new Hashtable();
			/*RadioButtonList list1=(RadioButtonList)Panel1.FindControl("rbtnList1");
			string ans1="";
			if(Page.IsPostBack)
				ans1+="back";
			if(list1!=null)
			ans1=list1.SelectedValue.ToString();
			
			//RadioButtonList list;
			string ans="";
			ans=ans1;*/
			string ans="";
		for(int i=1;i<=50;i++)
			{RadioButtonList list;
			if(i<=25)
			{
	          list=(RadioButtonList)Panel1.FindControl("rbtnList"+i.ToString());
			}else list=(RadioButtonList)Panel2.FindControl("rbtnList"+i.ToString());
			 if(list!=null) 
			{    ans=list.SelectedValue.ToString();
				 if(!ht_stuAnswer.Contains(i.ToString()))
				ht_stuAnswer.Add(i.ToString(),ans);
			}else ans+=i.ToString()+"null";
			}
		Session["stuAnswer"]=ht_stuAnswer;
		Session["ans"]=ans;
		Response.Redirect("result.aspx?stuId="+Convert.ToString(Session["stuId"])+"courseId="+Convert.ToString(Session["courseId"]));
		}

		
	}
}

⌨️ 快捷键说明

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