標籤:nic return load bytes static .net body 建議 cat
在閱讀時如有問題或者建議,歡迎指出和提問,我也是初學者.........
前台代碼:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <div align="center"> <h1>使用者登入頁面</h1> <form id="form1" runat="server"> <p> <asp:Label ID="lbusername" runat="server">學號:</asp:Label> <asp:TextBox ID="txtNum" runat="server"></asp:TextBox> </p> <p> <asp:Label ID="lblName" runat="server">姓名:</asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </p> <p> <asp:Label ID="lbpsw" runat="server">密 碼:</asp:Label> <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox> </p> <p> <asp:Label ID="lblText" runat="server" Text=""></asp:Label></p> <p> <asp:Button ID="btnRegister" runat="server" Text="註冊" onclick="btnRegister_Click" /> </p> </form> </div> </body> </html>
後台代碼:
using System;using System.Data.SqlClient;using System.Data;using System.Configuration;using System.Drawing;using System.Text;using System.Security.Cryptography;namespace ado.netDemo1{ public partial class register : System.Web.UI.Page { SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString()); string sql; protected void Page_Load(object sender, EventArgs e) { } protected void btnRegister_Click(object sender, EventArgs e) { if (txtNum.Text.Trim() == "" || txtName.Text.Trim() == "" || txtpwd.Text.Trim() == "") { lblText.ForeColor = Color.Red; lblText.Text = "請將個人資訊填寫完整!"; } else if (txtNum.Text.Trim() == "" && txtName.Text.Trim() == "" && txtpwd.Text.Trim() == "") { lblText.ForeColor = Color.Red; lblText.Text = "請將個人資訊填寫完整!"; } else { //加密處理註冊Password string hashedpwd=Encrypt(txtpwd.Text.Trim()); sql = "insert into tb_Students(SID,Name,Password)Values(‘" + txtNum.Text.Trim() + "‘,‘" + txtName.Text.Trim() + "‘,‘" + hashedpwd + "‘)"; connStr.Open(); SqlCommand cmd = new SqlCommand(sql, connStr); int i = cmd.ExecuteNonQuery(); if (i == 1) { lblText.ForeColor = Color.Red; lblText.Text = "註冊成功!"; Response.Write(@"<script>window.alert(‘請到登陸介面登陸!‘);window.location=‘Login.aspx‘</script>"); } } } public static string Encrypt(string cleanString) { Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString); Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes); return BitConverter.ToString(hashedBytes); } }}
介面:
【ADO.NET基礎-Regidter】簡單的賬戶註冊介面和原始碼(可用於簡單面試基礎學慣用)