How to access the Codebehind method of ASPX through Ajax

Source: Internet
Author: User
Tags json

Suddenly seen in the project, The Ajax in ASPX can access the methods in the Aspx.cs, feel very novel, perhaps LZ Shaojianduoguai, however, really have found the new world like that kind of excitement, you may know what this means, learned this way, on behalf of you, you can build fewer pages, less ashx, less ... Can you not be excited? In the LZ's impression, Ajax is generally associated with the general handler, request another ASPX page does not say, and the request of their own Codebehind method is really rarely seen, here record, perhaps there is the same as I do not know friends, I hope to help you. It's just that Ajax can access the WebMethod feature in WebService, and it's not possible to access the method of adding WebMethod in aspx. Thanks for knowing now.

Ajax Configuration

Test: Simulate an addition operation in the second grade of primary school, pass the value to the Add method in Default.aspx.cs by Ajax, and return the result by this method.

Default.aspx

How to use Ajax here is not much to say.

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Wolfy.AjaxSelf.Default"% > <! DOCTYPE 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; }
    }
}

Test results

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/ajax/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.