ASP.NET登入註冊頁面實現_實用技巧

來源:互聯網
上載者:User

如何利用Visual  studio 2010建立一個ASP網站?
【檔案】-->【建立】-->【網站】-->【ASP.Net網站】--【完成】

預設頁面Default.aspx
建立以上介面:

<%@ 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">  <h1>使用者登入頁面</h1>  <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> 

登入驗證功能

/// <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>");  } 

知識點歸納:
1.如何建立一個ASP.Net網站
2. 使用伺服器控制項實現介面設計
3. 對資料庫的簡單操作

一個簡單的使用者註冊、登入就完成了,通過這個案例希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.