如何通過ajax訪問aspx的CodeBehind中的方法

來源:互聯網
上載者:User

在項目中突然看到,aspx中的ajax可以訪問aspx.cs中的方法,覺得很新奇,也許是lz少見多怪,不過,真的有發現新大陸似的那種興奮,你也許知道這代表什麼,學會了這種方式,代表你以後,可以建更少的頁面,更少ashx,更少的.....能不興奮嗎?在lz的印象中,ajax一般都和一般處理常式聯絡起來的,請求另外的aspx頁面就不說了,而請求自身CodeBehind中的方法真的很少見,這裡記錄一下,也許也有跟我一樣不知道的朋友,希望能幫到你。只是知道ajax可以訪問webservice中加webmethod特性的方法,沒想到也可以訪問aspx中加webmethod的方法。幸虧現在知道了。

ajax配置

測試:類比一個小學二年級的加法運算,通過ajax將值傳給default.aspx.cs中的Add方法,通過該方法完成計算,返回結果。

Default.aspx

ajax的使用方法這裡就不多說了。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Wolfy.AjaxSelf.Default" %>    <!DOCTYPE html>    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"  />    <title></title>    <script type="text/javascript" src="jquery-1.10.2.js"></script>    <script type="text/javascript">        $(function () {            $("#btn").click(function () {                var num1 = $("#num1").val();                var num2 = $("#num2").val();                $.ajax({                    type: "POST",//                    url: "Default.aspx/Add",                    data: "{num1:'"+num1+"',num2:'"+num2+"'}",                    contentType: "application/json",                    dataType: "json",                    success: function (data) {                        $("#num3").val(data.d);                    },                    error: function (msg) {                        alert(msg.status);                    }                });            });        });    </script></head><body>    <form id="form1" runat="server">        <div>            <input type="text" id="num1" name="name" value="1"  />            <label>+</label>            <input type="text" id="num2" name="name" value="1"  />            <input type="button" name="name" id="btn" value="="  />            <input type="text" id="num3" name="name" value="1"  />            </div>    </form></body></html>

Default.aspx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.UI;using System.Web.UI.WebControls;namespace Wolfy.AjaxSelf{    public partial class Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            }        [WebMethod]        public static int Add(int num1, int num2)        { return num1 + num2; }    }}

測試結果

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/ajax/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.