首先分別新建三個網頁,register.aspx,login.aspx, yzm.aspx分別用于用戶的注冊,登錄以及驗證碼的生成。
register.aspx前臺代碼:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="_Default" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title>注冊頁面</title> 9 <style type ="text/CSS">10 .wrap {11 padding-top:40px;12 margin:0 auto;13 width:760px;14 height:426px;15 }16 .auto-style1 {17 width: 399px;18 }19 </style>20 </head>21 <body>22 <form id="form1" runat="server">23 <table>24 <tr>25 <td>用戶名:</td><td class="auto-style1"><asp:TextBox ID ="txtName" runat ="server" AutoPostBack="true" OnTextChanged ="txtName_TextChanged"></asp:TextBox><asp:Label ID ="labuser" runat ="server"></asp:Label></td>26 </tr>27 <tr>28 <td>密碼:</td><td class="auto-style1"><asp:TextBox ID ="txtPass" runat ="server" TextMode ="PassWord" Width="149px"></asp:TextBox></td>29 </tr>30 <tr>31 <td>重復密碼:</td><td class="auto-style1"><asp:TextBox ID ="txtQpass" runat ="server" TextMode ="Password" Width="149px"></asp:TextBox></td>32 </tr>33 <tr>34 <td>昵稱:</td><td class="auto-style1"><asp:TextBox ID ="txtNickName" runat ="server"></asp:TextBox></td>35 </tr>36 <tr>37 <td>性別:</td><td class="auto-style1"><asp:RadioButton ID ="radnan" runat ="server" Text ="男" GroupName ="sex" Width ="80px"/><asp:RadioButton ID ="radnv" runat ="server" Text ="女" GroupName ="sex" Width ="80px"/></td>38 </tr>39 <tr>40 <td>電話:</td><td class="auto-style1"><asp:TextBox ID ="txtPhone" runat ="server"></asp:TextBox></td>41 </tr>42 <tr>43 <td>E-mail:</td><td class="auto-style1"><asp:TextBox ID ="txtEamil" runat ="server"></asp:TextBox></td>44 </tr>45 <tr>46 <td>所在城市:</td><td class="auto-style1"><asp:TextBox ID ="txtCity" runat ="server"></asp:TextBox></td>47 </tr>48 <tr>49 <td><asp:Button ID ="btnregister" runat ="server" Text ="注冊" OnClick ="btnregister_Click"/></td><td class="auto-style1"><asp:Button ID ="btnreturn" runat ="server" Text="返回" PostBackUrl ="~/Default.aspx" CausesValidation ="true"/></td>50 </tr>51 </table>52 </form>53 </body>54 </html>
register.aspx.cs后臺代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 using System.Data; 9 using System.Web.Security; 10 using System.Configuration; 11 using System.Data.SqlClient; 12 using System.Text.RegularExPRessions; 13 14 public partial class _Default : System.Web.UI.Page 15 { 16 protected void Page_Load(object sender, EventArgs e) 17 { 18 } 19 20 /// <summary> 21 /// 當焦點移出文本框時,判斷用戶是否存在,以及輸入的字符格式是否符合規定. 22 /// </summary> 23 /// <param name="sender"></param> 24 /// <param name="e"></param> 25 protected void txtName_TextChanged(object sender, EventArgs e) 26 { 27 if (this.txtName.Text == "") 28 { 29 //如果沒有輸入用戶名,則提示用戶名不能為空. 30 this.labuser.Text = " * 用戶名稱不能為空!"; 31 32 //改變提示lable控件文字的顏色. 33 this.labuser.ForeColor = System.Drawing.Color.Red; 34 } 35 else 36 { 37 if (isnameFormar())//調用isnameFormar方法,判斷用戶名是否符合格式要求. 38 { 39 if (isName())//調用iaName方法,判斷用戶名是否已經存在. 40 { 41 //已經注冊,則提示. 42 this.labuser.Text = " * 該用戶已經注冊!"; 43 44 //改變提示lable控件文字的顏色. 45 this.labuser.ForeColor = System.Drawing.Color.Red; 46 } 47 else 48 { 49 //沒有注冊,則提示可以注冊. 50 this.labuser.Text = " * 可以注冊!"; 51 52 //改變提示lable控件文字的顏色. 53 this.labuser.ForeColor = System.Drawing.Color.Red; 54 } 55 } 56 else 57 { 58 //格式不對,則提示 59 this.labuser.Text = " * 用戶名格式不對!"; 60 61 //改變提示lable控件文字的顏色. 62 this.labuser.ForeColor = System.Drawing.Color.Red; 63 } 64 } 65 } 66 67 /// <summary> 68 /// 注冊按鈕的事件 69 /// </summary> 70 /// <param name="sender"></param> 71 /// <param name="e"></param> 72 protected void btnregister_Click(object sender, EventArgs e) 73 { 74 if (isnameFormar()) 75 { 76 if (isName()) 77 { 78 this.labuser.Text = "用戶名已經存在!"; 79 80 this.labuser.ForeColor = System.Drawing.Color.Red; 81 82 RegisterStartupScrmd5進行加密. 90 string userpass = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPass.Text, "MD5");//不報錯,但是已過時,在.NET3.5和4.0中有更好的方法替代他們。 91 92 //獲取昵稱 93 string nickname = this.txtNickName.Text; 94 95 //獲取性別 96 string sex = string.Empty; 97 if (this.radnan.Checked) 98 { 99 sex = "男";100 }101 else if (this.radnv.Checked)102 {103 sex = "女";104 }105 else { }106 107 //獲取電話號碼108 string phone = this.txtPhone.Text;109 110 //獲取電子郵件地址111 string Email = this.txtEamil.Text;112 113 //獲取所在城市114 string city = this.txtCity.Text;115 116 string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;//連接數據庫.117 118 using (SqlConnection con = new SqlConnection(strcon))119 {120 if (con.State == ConnectionState.Closed)121 {122 con.Open();123 }124 125 SqlParameter[] paras = new SqlParameter[] 126 {127 new SqlParameter("@username", username),128 new SqlParameter("@userpass", userpass),129 new SqlParameter("@nickname", nickname),130 new SqlParameter("@sex", sex),131 new SqlParameter("@phone", phone),132 new SqlParameter("@Email", Email),133 new SqlParameter("@city", city)134 };135 136 string sql = "insert into tb_User (userName, userPass, nickName, sex, phone, E_mail, city) values (@username, @userpass, @nickname, @sex, @phone, @Email, @city);";137 138 SqlCommand cmd = new SqlCommand(sql, con);139 140 cmd.Parameters.AddRange(paras); 1
新聞熱點
疑難解答