Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9153
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-15 15:04
个人简介

计C学生党,HTML+CSS+DIV 出门,熟悉php,asp.net 熟悉Clanguage,Java , C# ,掌握初阶网络知识,目前主要学习:UNIX 网络编程、Linux/UNIX 系统编程、ACM

文章分类

全部博文(7)

文章存档

2016年(7)

我的朋友
最近访客

分类: C#/.net

2016-05-16 19:18:20


点击(此处)折叠或打开

  1. /**
  2. *qau 555 2015.11 博盈杯
  3. *//这里代码应用场景为多类用户登录,根据用户选择不同的单选按钮判断用户登录的类型,
  4. *//从而进行不同的数据表进行判断,用户的用户名和密码是否正确。
  5. **/
  6. public partial class _Default : System.Web.UI.Page
  7. {
  8.     public string USER = "", PASSWORD = "";//过滤用户的输入
  9.     public int TYPE = -1;//验证用户选择的类型,初始值-1
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.         if (!IsPostBack)
  13.         {
  14.             username.Text = "";
  15.             password.Text = "";
  16.         }
  17.     }
  18.     protected void Button1_Click(object sender, EventArgs e)//判断用户选择的身份,进行相应的调用函数
  19.     {
  20.         switch (xz.SelectedValue)
  21.         {
  22.             case "1": login(1); break;
  23.             case "2": login(2); break;
  24.             case "3": login(3); break;
  25.         }
  26.     }
  27.     protected void Button2_Click(object sender, EventArgs e) //根据用户选择的身份,跳转到相应的注册页面
  28.     {
  29.         switch (xz.SelectedValue)
  30.         {
  31.             case "1":Server.Transfer("zhuce.aspx"); break;
  32.             case "2": Server.Transfer("teachaerzhuce.aspx"); break;
  33.             case "3": Response.Write(" alert('管理员注册未开放!'); "); break;
  34.         }
  35.         
  36.     }
  37.     protected void login(int TYPE)//传人用户选择的类型,执行不同的操作
  38.     {
  39.         string Type = "";
  40.         switch (TYPE)
  41.         {
  42.             case 1: Type = "同学"; break;
  43.             case 2: Type = "老师"; break;
  44.             case 3: Type = "adminsitrator"; break;
  45.         }
  46.         USER = username.Text;
  47.         PASSWORD = password.Text;
  48.         if (yz(TYPE, USER, PASSWORD))//调用yz()函数进行判断
  49.         {
  50.             if (Type != "adminsitrator")
  51.             Response.Write(" alert('欢迎 " + Type + " 回家!'); ");
  52.             Session["id"] = USER.ToString() ;
  53.             Session["bz"] = "yes";//标志
  54.             switch (TYPE)
  55.             {
  56.                 case 1: Server.Transfer("students.aspx"); break;
  57.                 case 2: Server.Transfer("teacher.aspx"); break;
  58.                 case 3: Response.Write(" alert('Sorry,administrator 回家了!'); "); break;
  59.             }
  60.         }
  61.         else
  62.         {
  63.             Response.Write(" alert('sorry,username or password error!'); ");

  64.         }

  65.     }
  66.     protected bool yz(int TYPE, string USER, string PASSWORD)//根据选则的用户身份,进行验证数据库,成功TRUE,失败FALSE
  67.     {
  68.         string Type = "";
  69.         switch (TYPE)
  70.         {
  71.             case 1: Type = "stu_"; break;
  72.             case 2: Type = "tea_"; break;
  73.             case 3: Type = "admin_"; break;
  74.         }
  75.         string sqlstr = "select * from " + Type + " where " + Type + "user='" + USER + "' and " + Type + "pass='" + DB.MD5Encrypt(PASSWORD) + "';";
  76.         DB db = new DB();//DB类用来对数据库的操作
  77.         MySqlDataReader selectcom = db.Select1(sqlstr);
  78.         while (selectcom.Read())
  79.         {
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84. }

阅读(329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~