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

来源:互联网 发布:mac jenkins 自动启动 编辑:程序博客网 时间:2024/05/11 02:29

这里用到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代码:

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

<headrunat="server">

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

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

    <styletype="text/css">

    .color{border:2pxsolid Red;}

    </style>

    <scripttype="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>

    <formid="form1" runat="server">

    <asp:ScriptManagerID="ScriptManager1"runat="server">

    <Services>

    <asp:ServiceReferencePath="~/VerifyUserName.asmx"/>

    </Services>

    </asp:ScriptManager>

    <div>

   

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

       

        </div>

    </form>

</body>

</html>

 

 

原创粉丝点击