ASP.NET & Jquery

来源:互联网 发布:北京排名优化公司 编辑:程序博客网 时间:2024/06/05 05:44

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" onclick="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 onclick="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();        }

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











原创粉丝点击