温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用ASP.NET怎么编写一个登录注册页面

发布时间:2021-02-08 17:09:17 来源:亿速云 阅读:1444 作者:Leah 栏目:开发技术

使用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"> 
 <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>

登录验证功能

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

关于使用ASP.NET怎么编写一个登录注册页面问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI