📄 user.aspx.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.Data.SqlClient;
namespace myResult
{
/// <summary>
/// user 的摘要说明。
/// </summary>
public class user : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtuser;
protected System.Web.UI.WebControls.TextBox txtpwd;
protected System.Web.UI.WebControls.Button btnlogin;
protected System.Web.UI.WebControls.DropDownList ddltype;
protected System.Web.UI.WebControls.Button btnclear;
SqlConnection conn;
SqlCommand myCmd;
protected System.Web.UI.WebControls.ValidationSummary vsShow;
protected System.Web.UI.WebControls.RegularExpressionValidator rev;
protected System.Web.UI.WebControls.Label lb_message;
SqlDataReader myDr;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private void ExecuteSelect()
{
string str_user=this.txtuser.Text.Trim().Replace("'","''");
string str_pwd=this.txtpwd.Text.Trim().Replace("'","''");
string str_type=this.ddltype.SelectedValue.ToString();//用户类型
conn=connDB.createConn();
string str_sel1="select StudentId from Student where Snumber=@Anumber and Spassword=@Apassword";//从学生表查询
string str_sel2="select TeacherId from Teacher where Tnumber=@Anumber and Tpassword=@Apassword";//从老师表进行查询
conn.Open();
if(str_type=="学生")
{
myCmd=new SqlCommand(str_sel1,conn);
}
else
{
myCmd=new SqlCommand(str_sel2,conn);
}
myCmd.Parameters.Add(new SqlParameter("@Anumber",SqlDbType.Int));
myCmd.Parameters["@Anumber"].Value=str_user;
myCmd.Parameters.Add(new SqlParameter("@Apassword",SqlDbType.VarChar,50));
myCmd.Parameters["@Apassword"].Value=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str_pwd,"MD5").ToString();//密码加密
myDr=myCmd.ExecuteReader();
if(myDr.Read())
{
Session["user"]=this.txtuser.Text.Trim();
if(str_type=="学生")
{
Session["id"]=myDr["StudentId"].ToString().Trim();
Response.Redirect("student.aspx");//跳转到学生页面
}
else
{
Session["id"]=myDr["TeacherId"].ToString().Trim();
Response.Redirect("teacher.aspx");//跳转到老师页面
}
}
else
{
string Dhtmlalert="";
Dhtmlalert += "<script language=javascript>";
Dhtmlalert += "alert('编号或密码错误,请重新输入');";
Dhtmlalert += "</script>";
this.lb_message.Text=Dhtmlalert;
}
myDr.Close();
conn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnlogin.Click += new System.EventHandler(this.btnlogin_Click);
this.btnclear.Click += new System.EventHandler(this.btnclear_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnlogin_Click(object sender, System.EventArgs e)
{
if(this.txtuser.Text==""||this.txtpwd.Text=="")
{
this.lb_message.Text="<script language='javascript'>alert('编号和密码不能为空!');</script>";
}
else
{
if(ddltype.SelectedIndex<1)
{
this.lb_message.Text="<script>alert('请选择用户类型!');</script>";
}
else
{
this.ExecuteSelect();
}
}
}
private void btnclear_Click(object sender, System.EventArgs e)
{
this.txtuser.Text="";
this.txtpwd.Text="";
this.ddltype.SelectedIndex=0;//回到初始索引
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -