溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用ASP.NET怎么編寫一個(gè)登錄注冊頁面

發(fā)布時(shí)間:2021-02-08 17:09:17 來源:億速云 閱讀:1485 作者:Leah 欄目:開發(fā)技術(shù)

使用ASP.NET怎么編寫一個(gè)登錄注冊頁面?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

默認(rèn)頁面Default.aspx

創(chuàng)建以上界面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserLogin.aspx.cs" Inherits="UserLogin" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
 <title></title> 
</head> 
<body> 
 <div align="center"> 
 <h2>用戶登錄頁面</h2> 
 <form id="form1" runat="server"> 
  <p> <asp:Label ID="lbusername" runat="server">用戶名:</asp:Label> 
   <asp:TextBox ID="tbusername" runat="server"></asp:TextBox> 
   </p> 
 
  <p> <asp:Label ID="lbpsw" runat="server">密 碼:</asp:Label> 
  <asp:TextBox ID="tbpsw" runat="server" TextMode="Password"></asp:TextBox> 
  </p> 
  <p><asp:Button ID="btnLogin" runat="server" Text="登錄" onclick="btnLogin_Click" /> 
   <asp:Button ID="btnCancel" runat="server" Text="取消" onclick="btnCancel_Click" /> 
  </p> 
 </form> 
 </div> 
</body> 
</html>

登錄驗(yàn)證功能

/// <summary> 
 /// 用戶登錄 
 /// </summary> 
 /// <param name="sender"></param> 
 /// <param name="e"></param> 
 protected void btnLogin_Click(object sender, EventArgs e) 
 { 
  if (tbusername.Text == "") 
  { 
   Response.Write(@"<script>alert('用戶名不能為空!');</script>"); 
  } 
  if (tbpsw.Text == "") 
  { 
   Response.Write(@"<script>alert('密碼不能為空!');</script>"); 
  } 
  string username = tbusername.Text; 
  string password = tbpsw.Text; 
  string sql = "select * from T_user where username=@username and password=@password"; 
  SqlParameter[] parameters = { new SqlParameter("@username",username), new SqlParameter("@password", password) }; 
  using (SqlConnection conn = new SqlConnection(connstr)) 
  { 
   conn.Open(); 
   using (SqlCommand cmd = conn.CreateCommand()) 
   { 
    cmd.CommandText = sql; 
    cmd.Parameters.AddRange(parameters); 
    DataSet ds = new DataSet(); 
    SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
    adapter.Fill(ds); 
    DataTable table = ds.Tables[0]; 
    if(table != null) 
     Response.Write(@"<script>alert('登錄成功!');</script>"); 
    else 
     Response.Write(@"<script>alert('登錄失??!');</script>"); 
 
   } 
  } 
 
   
 }

注冊功能

/// <summary> 
 /// 用戶注冊 
 /// </summary> 
 /// <param name="sender"></param> 
 /// <param name="e"></param> 
 protected void brnRegister_Click(object sender, EventArgs e) 
 { 
  string username = tbusername.Text; 
  string password = tbpsw.Text; 
  string sql = "Insert into T_user(username, password) values(@username,@password)"; 
  SqlParameter[] parameters = { new SqlParameter("@username", username), new SqlParameter("@password", password) }; 
  using (SqlConnection conn = new SqlConnection(connstr)) 
  { 
   conn.Open(); 
   using (SqlCommand cmd = conn.CreateCommand()) 
   { 
    cmd.CommandText = sql; 
    cmd.Parameters.AddRange(parameters); 
    cmd.ExecuteNonQuery(); 
   } 
  } 
 
  Response.Write(@"<script>alert('注冊成功!');</script>"); 
 }

關(guān)于使用ASP.NET怎么編寫一個(gè)登錄注冊頁面問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI