微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ajax 使用net独有控件ScriptManager实现无刷新效果【登陆】

web窗体------WebForm1.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtUserName').blur(function () {
                WebAjax.WebService1.ValidateUser($(this).val(),onSuccess,onFailed);
            })

            function onSuccess(result) {
                $('#spinfo').text(result);
            }
            function onFailed() {
                $('#spinfo').text('在验证您的用户名是否可用时发生错误,请稍后再试,请反馈到某邮箱');
            }
        })


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
    <asp:ServiceReference Path="WebService1.asmx" />
    </Services>
    </asp:ScriptManager>
    <div>
    用户名<input type="text" id="txtUserName" /><span id="spinfo"></span><br />
    密码<input type="text" id="txtPwd" />
    </div>
    </form>
</body>
</html>

wed服务---------------WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebAjax
{
    /// <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
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string ValidateUser(string username)
        {
            if (username == "onlifes")
            {
                return "用户名已被占用,请选择其他";
            }
            else
            {
                return "可以使用,请继续";
            }
        }
        [WebMethod]
        public string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }
    }
}

原文地址:https://www.jb51.cc/ajax/166366.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐