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

这里用到ASP中的WebService与实现界面的数据交换!

 

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

 

WebService页面代码:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Data.SqlClient;

 

namespace AJAX

{

    ///<summary>

    /// VerifyUserName Ì?a°a¦Ì¡Â

    ///</summary>

    [WebService(Namespace= "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    // ¨?°a¨º¨ª使º1®? ASP.NET AJAX 䨮?À?D̡®?ä? Web ¤t?ê??¨????DÌ?Á¡éº¨ª¡ê

    [System.Web.Script.Services.ScriptService]

    public class VerifyUserName: System.Web.Services.WebService

    {

        stringstr = System.Configuration.ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;

 

        [WebMethod]

        public string HelloWorld()

        {

            return"Hello World";

        }

        [WebMethod]

        public string GetUser(stringusername)

        {

            SqlConnectionsqlCnn = new SqlConnection(str);

            SqlCommandsqlCmm = new SqlCommand("select count(*) from UserInfo whereUserName=@username", sqlCnn);

            sqlCmm.Parameters.AddWithValue("@username",username);

            sqlCnn.Open();

            objectobj = sqlCmm.ExecuteScalar();

            sqlCmm.Dispose();

            sqlCnn.Dispose();

            returnobj.ToString();

        }

    }

}

 

 

 

实现页面html代码:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>验¨¦证¡è用®?户¡ì名?是º?否¤?存ä?在¨²</title>

    <script src="Jquery1.7.js" type="text/javascript"></script>

    <style type="text/css">

    .color{border:2px solid Red;}

    </style>

    <script type="text/javascript">

        $(function() {

            $('#txtUserName').focus(function () {

                $(this).addClass('color');

                $(this).val('');

            })

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

                $(this).removeClass('color');

                if($(this).val() != ""){

                   AJAX.VerifyUserName.GetUser($(this).val().toString(),onSuccess, onFailed);

                }

            })

            functiononSuccess(e) {

                if(parseInt(e) > 0) {

                    $('span').text('?®?¡ì?°?Á¡é¨¢ê?').css('color', 'Red');

 

                }

                else{

                    $('span').text('?®?¡ì?¨¦°?使º1®?ê?').css('color', 'Green');

                }

            }

            functiononFailed() {

                alert('GameOver!');

            }

        })

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    <Services>

    <asp:ServiceReference Path="~/VerifyUserName.asmx" />

    </Services>

    </asp:ScriptManager>

    <div>

   

        用户名?:<input id="txtUserName" type="text" value="请输入用户名"/>&nbsp;&nbsp;<span></span>

       

        </div>

    </form>

</body>

</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值