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

📄 webform1.aspx.cs

📁 利用C#编写一个简单的考试系统。主要功能是
💻 CS
字号:
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.Configuration;

using Teach_Manager.Dao;
using Teach_Manager.Common; 
namespace Teach_Manager
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : PageBase 
	{
		protected System.Web.UI.WebControls.TextBox txtUserName;
		protected System.Web.UI.WebControls.Label lblUserName;
		protected System.Web.UI.WebControls.Label lblPassword;
		protected System.Web.UI.WebControls.Button btn_1;
		protected System.Web.UI.WebControls.Button btn_2;
		protected System.Web.UI.WebControls.TextBox txtPwd;
		protected System.Web.UI.WebControls.RadioButtonList select_Class;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			//清空所有的Cookie值
			if (!Page.IsPostBack)
			{
//                Response.Cookies["UserName"].Value="";
//			    Response.Cookies["connstr"].Value="";
//
//                Response.Cookies["class_type"].Value="";
			}
			
		}

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

		}
		#endregion

		private void btn_1_Click(object sender, System.EventArgs e)
		{
			//确认 这里需要确认用户名和密码都不能为空
			try
			{

//				Response.Cookies["UserName"].Value="";
//				Response.Cookies["connstr"].Value="";
//
//				Response.Cookies["class_type"].Value="";


				int i=select_Class.SelectedIndex;
				string str_Url="";
				
				string p_PassStr =txtPwd.Text.Trim().Replace("'","''"); 
				string p_strName=txtUserName.Text.Trim().Replace("'","''");
				string str_Conn=ConfigurationSettings.AppSettings["ConnStr"].ToString();//
				if ((p_PassStr=="")||(p_strName==""))
				{
					Response.Write("<script language=javascript>window.alert('用户名和密码不能为空!');</script>");
					return;
				}
				
				string str_sql="";
				if (i==0)
				{
					str_sql=" select * from teacher_table where IsDelete='0' and  ID='"+p_strName+"' and Password='"+p_PassStr+"'";
				}
				else
				{
                    str_sql=" select * from student_table where IsDelete='0' and  ID='"+p_strName+"' and Password='"+p_PassStr+"'";
				}
				
				
				CommonInterface CIF=new CommonSql(str_Conn);  
				DataTable dtl=CIF.ExeForDtl(str_sql);
				if(dtl.Rows.Count>0)
				{
					switch (i)
					{
						case 0:
							//教师页面
							//Response.Write("<script language=javascript>window.alert('进入教师页面!');</script>");
							str_Url="teacher/teacher_Page1.aspx";
							break;
						case 1:
							//学生页面
							//Response.Write("<script language=javascript>window.alert('进入学生页面!');</script>");
							str_Url="student/student_Page1.aspx";
							break;                
					}		
					
					//用户名称
					HttpCookie MyCookie=new HttpCookie("username");
					MyCookie.Value=p_strName;
					


					HttpCookie MyCookie3=new HttpCookie("username_guid");
                    MyCookie3.Value=dtl.Rows[0]["guid"].ToString(); 


					//数据库连接字符串
//					HttpCookie MyCookie1=new HttpCookie("connstr");
//                    MyCookie1.Value=str_Conn;
                    
					Session["str_Conn"]=str_Conn;
                    
					//类型 0 教师  1 学生
					HttpCookie MyCookie2=new HttpCookie("class_type");
					MyCookie2.Value=i.ToString();

					Response.Cookies.Add(MyCookie);
//					Response.Cookies.Add(MyCookie1);

					str_conn_protected=str_Conn;
					Response.Cookies.Add(MyCookie2);


					Response.Cookies.Add(MyCookie3);

					#region 写登陆记录表
                    string str_sql_insert="insert into log_table(IPAddress,user_name,class,login_datetime) ";
					str_sql_insert+="values (";
					str_sql_insert+="'"+Request.UserHostAddress.ToString()+"',";
					str_sql_insert+="'"+txtUserName.Text.Trim() +"',";
					str_sql_insert+="'"+i.ToString()+"',";
					str_sql_insert+="'"+System.DateTime.Now.ToString() +"'";
					str_sql_insert+=")";

					
					CIF.Execute(str_sql_insert);

                    #endregion
					CIF.Close();
					Response.Write("<script>window.top.location.href('"+str_Url+"');</script>");		
				}
				else
				{
                    CIF.Close();
                    Response.Write("<script language=javascript>window.alert('用户名或密码错误!');</script>");
				}

				

			}
			catch
			{
                return;
			}
			

		}

		private void btn_2_Click(object sender, System.EventArgs e)
		{
			//取消
			Response.Cookies["UserName"].Value="";
			Response.Cookies["connstr"].Value="";
			
			Response.Cookies["class_type"].Value="";

			Response.Write("<script language=javascript>");
			//Response.Write("window.alert('您没有通过系统登录验证!');self.opener=null;self.close();");
			Response.Write("self.opener=null;self.close();");
			Response.Write("</script>");
		}
	}
}

⌨️ 快捷键说明

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