现在的位置: 首页 > 综合 > 正文

无刷新验证用户名是否存在

2018年04月18日 ⁄ 综合 ⁄ 共 3831字 ⁄ 字号 评论关闭

前台HTML文件代码(Register.aspx):

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            function CheckCode() {
                AJAX_Work.WebService1.CheckCode($('#txtName').val(), onSuccess, onFailed);
            }
            function onSuccess(result) {
                if (result == true) {
                    $('#yanzheng').append('<a style="color:green;"><samp>用户名可以使用!</samp></a>');
                                   }
                else {
                    $('#yanzheng').append('<a style="color:red;"><samp>用户名不可以使用!</samp></a>');
                    
                }
            }
            function onFailed() {
                alert('调用失败');
            }

            $('#txtName').blur(function () {

//调用上面的方法。
                CheckCode();
            })
        })

    </script>

//页面比较简单,表格也是鼠标拖成的这些是自动生成的一些个样式
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 157px;
        }
        .style3
        {
            width: 67px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>

//引用Web服务文件
            <asp:ServiceReference Path="~/WebService1.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <table class="style1">
            <tr>
                <td align="left" class="style3">
                    用户名:
                </td>
                <td class="style2">
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
                <td id="yanzheng">
                    <asp:Label ID="Label1" runat="server" ForeColor="Red" Text="*"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left" class="style3">
                    密&nbsp; 码:
                </td>
                <td class="style2">
                    <asp:TextBox ID="txtPass" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td align="left" colspan="2">
                    <asp:Button ID="Button2" runat="server" Text="注   册" XonClick="Button2_Click" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
    <div id="newdiv">
    </div>
    </form>
</body>
</html>


下面是Web服务文件的内容

Web服务文件(WebService1.asmx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;

namespace AJAX_Work
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
   [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        string str = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

//这里是查询数据库中是否有相同的记录,如果有则返回布尔值False表示当前的值是不可用的
        [WebMethod]
        public bool CheckCode(string name)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            SqlCommand sqlcom = sqlcon.CreateCommand();
            sqlcon.Open();
            sqlcom.CommandText = "select count(*) from T_Users where
Uname=@name";
            sqlcom.Parameters.AddWithValue("@name",name);
             int  obj=Convert.ToInt32( sqlcom.ExecuteScalar()); 
            sqlcon.Dispose();
              sqlcom.Dispose();
              if (obj != 0)
              {
                  return false;
              }
              else
              {
                  return true;
              }
        }
    }
}

页面功能较为简单,具有可扩展性!

【上篇】
【下篇】

抱歉!评论已关闭.