ASP.NET简单登录注册实例

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>
    <form id="form1" runat="server">
    <div>
       <table>
        <tr>
            <td> <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
          <tr>
            <td> <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
        </tr>
         <tr>
            <td><asp:Button ID="Button2" runat="server" Text="登录" onclick="Button2_Click" /> </asp:Label> </td>
            <td> <asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" /> </asp:TextBox></td>
        </tr>
       </table>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //登录
        protected void Button2_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            if (name!=null||pwd!=null)
            {
                string sql = "select count(0) from student where stuName=@a and stuNo=@b";//获取数据中的信息
                SqlParameter p1 = new SqlParameter("@a",name);
                SqlParameter p2 = new SqlParameter("@b",pwd);
                //连接数据库
                string connStr = "server=DESKTOP-QQGOIKH;uid=sa;pwd=123;database=stuDB";
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.Parameters.Add(p1);
                cmd.Parameters.Add(p2);
                object obj = cmd.ExecuteScalar();
                conn.Close();
                int i = (int)obj;
                if (i>0)
                {
                    Response.Redirect("WebForm2.aspx");
                }
                else
                {
                    Response.Write("登录失败");
                }
            }
            else
            {
                return;
            }
                
            
        }
        //注册
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm3.aspx");

        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>

<!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>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td><asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label> </td>
            <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label> </td>
            <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td>
        </tr>
        <tr>
            <td> <asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" /> </td>
            <td> <asp:Button ID="Button2" runat="server" Text="返回" onclick="Button2_Click" /> </td>
        </tr>
    </table> 
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //注册
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            if (name!=null||pwd!=null)
            {
                string sql = "insert into enter values(@a,@b)";
                SqlParameter[] sp = new SqlParameter[2];
                sp[0] = new SqlParameter("@a",TextBox1.Text);
                sp[1] = new SqlParameter("@b",TextBox2.Text);
                SqlConnection conn = new SqlConnection("server=STER-PC;uid=sa;pwd=123;database=t2");
                conn.Open();
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.Parameters.AddRange(sp);//添加数组
                int i = comm.ExecuteNonQuery();
                conn.Close();
                if (i>0)
                {
                    Response.Write("注册成功");
                }
                else
                {
                    Response.Write("注册失败");
                }
            }
            else
            {
                Response.Write("请填写内容");
            }
        }
        //返回
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm1.aspx");
        }
    }
}
原文地址:https://www.cnblogs.com/xiaz/p/5242536.html