• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET简单登录注册实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<%@ 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");
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET框架 数据回发与事件回发发布时间:2022-07-10
下一篇:
如何实现无刷新的DropdownList联动效果‖ASP.NET技巧发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap