📄 main.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 WebApplication
{
/// <summary>
/// Main 的摘要说明。
/// </summary>
public class Main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLinkSearch;
protected System.Web.UI.WebControls.HyperLink HyperLinkForum;
protected System.Web.UI.WebControls.HyperLink HyperLinkExplorer;
protected System.Web.UI.WebControls.TextBox TextBoxName;
protected System.Web.UI.WebControls.TextBox TextBoxPassword;
protected System.Web.UI.WebControls.Button ButtonEnter;
protected System.Web.UI.WebControls.Button ButtonLogin;
protected System.Web.UI.WebControls.TextBox TextBoxState;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
if(Session["userName"].ToString()!="")
{
this.TextBoxState.Text="用户"+Session["userName"].ToString()+"登录成功。";
SetEnable(true);
}
else
{
this.TextBoxState.Text="如果你已经注册过,请登录,如果尚未注册,请先注册!";
SetEnable(false);
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ButtonEnter.Click += new System.EventHandler(this.ButtonEnter_Click);
this.ButtonLogin.Click += new System.EventHandler(this.ButtonLogin_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SetEnable(bool enable)
{
this.HyperLinkExplorer.Enabled=enable;
this.HyperLinkForum.Enabled=enable;
this.HyperLinkSearch.Enabled=enable;
}
private void ButtonEnter_Click(object sender, System.EventArgs e)
{
string name=this.TextBoxName.Text;
string pwd=this.TextBoxPassword.Text;
if(name.IndexOf("'")>-1 || name.IndexOf(' ')>-1 || name.IndexOf('"')>-1
|| pwd.IndexOf("'")>-1 || pwd.IndexOf(' ')>-1 || pwd.IndexOf('"')>-1)
{
this.TextBoxState.Text="用户名或密码包含有非法字符!";
return;
}
SqlConnection conn = new SqlConnection(MyClass.ConnString);
string sqlstr="select * from loginuser where (username='"+name+"') and ("
+"userpwd='"+pwd+"')";
SqlCommand command = new SqlCommand(sqlstr,conn);
conn.Open();
try
{
SqlDataReader dr=command.ExecuteReader();
if(dr.Read()==true)
{
SetEnable(true);
Session["userName"]=name;
this.TextBoxState.Text="用户"+name+"登录成功。";
}
else
{
SetEnable(false);
Session["userName"]="";
this.TextBoxState.Text="登录失败,无此用户或密码不正确!";
}
dr.Close();
}
catch(Exception err)
{
this.TextBoxState.Text="系统错误:"+err.Message;
}
conn.Close();
}
private void ButtonLogin_Click(object sender, System.EventArgs e)
{
Response.Redirect("login.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -