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 == "[email protected]")
            {
                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();
        }

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











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章