📄 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 login
{
/// <summary>
/// register 的摘要说明。
/// </summary>
public class register : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected System.Web.UI.WebControls.Label Label7;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlCommand sqlCommand1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
this.Label1.Text=RandomNum(4);
}
}
public string RandomNum(int n) //
{
string strchar = "0,1,2,3,4,5,6,7,8,9" ;
string[] VcArray = strchar.Split(',') ;
string VNum = "" ;
//由于字符串很短,就不用StringBuilder了
int temp = -1 ;
//记录上次随机数值,尽量避免产生几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
Random rand =new Random();
for ( int i = 1 ; i < n+1 ; i++ )
{
if ( temp != -1)
{
rand =new Random(i*temp*(int) DateTime.Now.Ticks);
}
//int t = rand.Next(35) ;
int t=rand.Next(10);
// if (temp != -1 && temp == t)
// {
// return RndNum( n);
// }
temp = t ;
VNum += VcArray[t];
}
return VNum ;//返回生成的随机数
}
// ''函数名称:RndNum
// ''函数参数:VCodeNum--设定返回随机字符串的位数
// ''函数功能:产生数字和字符混合的随机字符串
// private string RndNum(int VcodeNum)
// {
// string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
// string[] VcArray = Vchar.Split(",".ToCharArray()); //将字符串生成数组
// string VNum = "";
// int i;
// for(i=1;i<VcodeNum;i++)
// {
// System.Random ro = new Random();
// double decA = ro.NextDouble();
// VNum = VNum + VcArray[Convert.ToInt32(35*decA)];
// }
// // For i=1 to VcodeNum
// // Randomize
// // VNum=VNum & VcArray(Int(35*Rnd)) ''数组一般从0开始读取,所以这里为35*Rnd
// // Next
// return VNum;
// }
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=ZLADEGOG;packet size=4096;user id=sa;data source=\"(local)\";persist" +
" security info=False;initial catalog=login";
//
// sqlCommand1
//
this.sqlCommand1.CommandText = "SELECT [user].* FROM [user]";
this.sqlCommand1.Connection = this.sqlConnection1;
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TextBox1.Text!=this.Label1.Text)
{
this.Label7.Text="注册码不符";
return;
}
//Response.Write(this.Label1.Text);
this.sqlConnection1.Open();
SqlDataReader myDataReader=this.sqlCommand1.ExecuteReader();
while(myDataReader.Read())
{
if(myDataReader.GetString(1).ToString().Trim()==this.TextBox2.Text.ToString().Trim())
{
Response.Write("<script>alert('已由该用户名,请重新注册')</script>");
this.sqlConnection1.Close();
return;
}
}
this.sqlConnection1.Close();
this.sqlCommand1.CommandText="insert into [user](username,passsword) values('"+this.TextBox2.Text.ToString().Trim()+"','"+this.TextBox3.Text.ToString().Trim()+"')";
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteNonQuery();
this.sqlConnection1.Close();
}
private void Button2_Click(object sender, System.EventArgs e)
{
this.TextBox1.Text="";
this.TextBox2.Text="";
this.TextBox3.Text="";
this.TextBox4.Text="";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -