ASP.NET & Jquery

AjaxResponse.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace SmsReminderApp
{
    public partial class AjaxResponse : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
     
        [WebMethod]
        public static string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }
}


AjaxTest.aspx

<div id="Result">Click here for the time.</div>
<script type="text/javascript">
    $(document).ready(function () {
        // Add the page method call as an onclick handler for the div.
        $("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "AjaxResponse.aspx/GetDate",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
        });
    });
</script>


########################################################################################################################

Back-end

public partial class AjaxResponse : System.Web.UI.Page
    {
        public static string[] UserNameArray;

        protected void Page_Load(object sender, EventArgs e)
        {
            UserNameArray = new string[7] { "testid01", "testid02", "testid03", "testid04", "testid05", "testid06", "testid07" }; 
        }

        [WebMethod]
        public static bool CheckUserName(string sUserName)
        {
            if (sUserName == "445756176@qq.com")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
}



Front-end

<a href="" name="links" id="links">Test Link</a>
    <br />
    <asp:textbox runat="server" ID="txtUserName" name="txtUserName"></asp:textbox>

    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $("#links").click(function (e) {
                e.preventDefault();
                if ($("#txtUserName").val() == '')
                    alert("Please enter the UserName");
                else
                    sendData($("#txtUserName").val());
            });
            function sendData(sUserName) {
                $.ajax({
                    type: "POST",
                    url: "AjaxResponse.aspx/CheckUserName",
                    data: '{"sUserName":"' + sUserName + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d)
                            alert("The User Name is valid");
                        else
                            alert("The User Name is invalid")
                    },
                    error: function () {
                        alert("An unexpected error has occurred during processing.");
                    }
                });
            }
        });
</script>


Onclick function

 <script type="text/javascript">
        var tbnext;
        function copyText(tbnext) {
            document.getElementById("txtUserName").value = tbnext;
        }
   </script>
    <a href="" name="links1" id="link1" οnclick="copyText('sss')">Test1 Link</a>
    <br />
    <asp:textbox runat="server" ID="txtUserName" name="txtUserName"></asp:textbox>


##########################################################################################################################

Transfer more parameters


Front-end

<script type="text/javascript">       
        var a;
        var b;
        function getdoubles(a,b) {
            $.ajax({
                type: "POST",
                url: "AjaxResponse.aspx/doublePars",
                data: '{a:"'+ a +'", b:"' + b +'"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
        }
       
   </script>

   <div οnclick="getdoubles(3,8)">ssss</div>

<div id="Result">Click here for the time.</div>


Back-end

 [WebMethod]
        public static string doublePars(int a , int b) // the web method must be static
        {
            int c = a + b;
            return c.ToString();
        }

#####################################################################################################################











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值