ASP.Net Ajax无刷新注册页面

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

数据库表:

数据库表内容

项目的数据模块图:

项目结构图

BLL层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication1.DAL;
using WebApplication1.Model;
namespace WebApplication1.BLL
{
    public class UserInfoBLL
    {
        UserInfoDAL userInfoDal = new UserInfoDAL();
        public UserInfo SelectUser(string userName)
        {
            return userInfoDal.SelectUser(userName);
        }
        public void Insert(UserInfo user)
        {
            userInfoDal.Insert(user);
        }
    }
}

DAL层:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using WebApplication1.Model;
namespace WebApplication1.DAL
{
    public class UserInfoDAL
    {
        SQLHelper sqlHelp = new SQLHelper();
        #region 查找用户
        public UserInfo SelectUser(string userName)
        {
            UserInfo user = new UserInfo();
            SqlDataReader reader = sqlHelp.ExecuteSqlReader("select * from T_login where username=@userName",
                new SqlParameter("@userName", userName));
            while (reader.Read())
            {
                user.userName = reader.GetString(1);
            }
            reader.Close();
            return user;
        }
        #endregion
        #region 用户注册
        public void Insert(UserInfo user)
        {
            sqlHelp.ExecuteNonQuery(@"INSERT INTO T_login
                       (username,password)
                        VALUES
                       (@UserName,@password)",
                new SqlParameter("@userName", user.userName),
                new SqlParameter("@password", user.password));
        }
        #endregion
    }
}

Model层(Entity类):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Model
{
    public class UserInfo
    {
        public string userName { get; set; }
        public string password { get; set; }
    }
}

UI层:

WebForm1.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="https://idaobin.com/test/jquery-3.2.1.js"></script>
    <script type="text/javascript">
        function JudgeUserName() {
            $.ajax({
                type: "GET",
                url: "UserHandler.ashx",
                dataType: "html",
                data: "userName=" + $("#txtUserName").val() + "&" + "pwd=" + $("#txtPwd").val(),
                beforeSend: function (XMLHttpRequest) {
                    $("#showResult").text("正在查询");
                    //Pause(this,100000);
                },
                success: function (msg) {
                    $("#showResult").html(msg);
                    $("#showResult").css("color", "red");
                },
                complete: function (XMLHttpRequest, textStatus) {
                    //隐藏正在查询图片
                },
                error: function () {
                    //错误处理
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtUserName" runat="server" onblur="JudgeUserName();"></asp:TextBox>
            <span id="showResult"></span>
        </div>
        <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
        <p>
            <input id="Button1" type="button" value="注册" onclick="JudgeUserName();" />
            </p>
    </form>
</body>
</html>

UserHandler.ashx代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication1.BLL;
using WebApplication1.Model;
namespace WebApplication1
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : IHttpHandler
    {
        UserInfo user = new UserInfo();
        UserInfoBLL userBll = new UserInfoBLL();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string userName = context.Request.QueryString["userName"].ToString();
            string password = context.Request.QueryString["pwd"].ToString();
            user = userBll.SelectUser(userName);
            if (userName == user.userName)
            {
                context.Response.Write("用户名已经存在!");
                return;
            }
            else if (userName=="")
            {
                context.Response.Write("用户名不能为空!");
                return;
            }
            else if (password=="")
            {
                context.Response.Write("密码不能为空!");
                return;
            }
            else
            {
                user.userName = userName;
                user.password = password;
                userBll.Insert(user);
                context.Response.Write("注册成功!");
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

还有数据库访问类(SQLHelper.cs)代码太多就不贴出来了

GitHub源码下载:https://github.com/kiritobin/NOReflushReg

原文链接:https://www.idaobin.com/archives/1268.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

88 − = 81