📄 register.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 Aspnet
{
/// <summary>
/// Register 的摘要说明。
/// </summary>
public class Register : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TB_UserName;
protected System.Web.UI.WebControls.TextBox TB_Pwd;
protected System.Web.UI.WebControls.TextBox TB_Pswd;
protected System.Web.UI.WebControls.TextBox TB_Email;
protected System.Web.UI.WebControls.RadioButtonList RBL_Sex;
protected System.Web.UI.WebControls.Button BT_submit;
protected System.Web.UI.WebControls.Button BT_reset;
protected System.Web.UI.WebControls.RequiredFieldValidator RFV_UserName;
protected System.Web.UI.WebControls.RequiredFieldValidator RFV_Pwd;
protected System.Web.UI.WebControls.CompareValidator CV_Pswd;
protected System.Web.UI.WebControls.RegularExpressionValidator REV_Email;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TB_RealName;
protected System.Web.UI.WebControls.TextBox TB_Telephone;
protected System.Web.UI.WebControls.TextBox TB_Address;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.TB_UserName.TextChanged += new System.EventHandler(this.TB_UserName_TextChanged);
this.BT_submit.Click += new System.EventHandler(this.BT_submit_Click);
this.BT_reset.Click += new System.EventHandler(this.BT_reset_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BT_submit_Click(object sender, System.EventArgs e)
{
//建立数据库连接
string connstr="server=.;database=Aspnet;uid=sa;pwd=sa";
string strsql="select count(*) from UserInfo where UserName like '"+TB_UserName.Text+"'";
SqlConnection Conn=new SqlConnection(connstr);
SqlCommand mycommand=new SqlCommand(strsql,Conn);
Conn.Open();
//看用户是否已经存在
int flag=(int)mycommand.ExecuteScalar();
Conn.Close();
if(flag==0)
{
//向数据库中添加一条用户记录
string WebSite;
WebSite=Request.UserHostAddress.ToString();//获得登录用户的IP
string selectstr="insert into UserInfo(UserName,Pwd,Sex,Email,Website,RegTime,RealName,Telephone,Address) values ('" +TB_UserName.Text +"','"+ TB_Pwd.Text + "','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text + "','"+WebSite+"',getdate(),'" +TB_RealName.Text +"','" +TB_Telephone.Text +"','" +TB_Address.Text +"')";
SqlCommand addcommand=new SqlCommand(selectstr,Conn);
Conn.Open();
addcommand.ExecuteNonQuery();
Conn.Close();
Session["Username"]=TB_UserName.Text;
Session["Sex"]=RBL_Sex.SelectedItem.Value;
Label1.Text="注册成功,你现在可以登录了!";
}
else
{
Label1.Text="对不起,该用户已经存在,请选择其他用户名!";
TB_UserName.Text="";
TB_Pwd.Text="";
TB_Pswd.Text="";
}
}
private void BT_reset_Click(object sender, System.EventArgs e)
{
Response.Redirect("Register.aspx") ;
}
private void TB_UserName_TextChanged(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -