ASP.NET登錄注冊頁面實現(xiàn)
如何利用Visual studio 2010創(chuàng)建一個ASP網(wǎng)站?
【文件】-->【新建】-->【網(wǎng)站】-->【ASP.Net網(wǎng)站】--【完成】

默認頁面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"> <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.如何創(chuàng)建一個ASP.Net網(wǎng)站
2. 使用服務器控件實現(xiàn)界面設計
3. 對數(shù)據(jù)庫的簡單操作
一個簡單的用戶注冊、登錄就完成了,通過這個案例希望對大家的學習有所幫助。
相關文章
asp.net Http異常eurl.axd出錯信息解決方法
在IIS6中同時啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會出現(xiàn)如下錯誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-08-08
ASP.NET泛型三之使用協(xié)變和逆變實現(xiàn)類型轉換
這篇文章介紹了ASP.NET使用協(xié)變和逆變實現(xiàn)泛型類型轉換的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
基于Asp.Net MVC4 Bundle捆綁壓縮技術的介紹
本篇文章,小編將為大家介紹,Asp.Net MVC4 Bundle捆綁壓縮技術,有需要的朋友可以參考一下2013-04-04
Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟
這篇文章主要介紹了Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟,幫助大家更好的理解和學習使用Asp.Net Core,感興趣的朋友可以了解下2021-03-03

