Summary of ASP. NET AJAX call background static method

Source: Internet
Author: User
1.Ajax Library Mode

C # code:

[WebMethod]
public static DateTime GetCurrentTime (String str)
{
return datetime.now;
}

JS Code:

<form id= "Form1" runat= "Server" >
<script language=javascript type= "Text/javascript" >
function GetCurrentTime1 () {
Pagemethods.getcurrenttime (' NewEgg Ajax Training ', checkissuccess);
}
function checkissuccess (Result) {
alert (result);
}
</script>
<div>
<asp:scriptmanager id= "ScriptManager1" runat= "Server" enablepagemethods= "true" >
</asp:ScriptManager>
<input id= "Button1" type= "button" value= "client control calls the server-side method" onclick= "GetCurrentTime1 ()"/>
</div>
</form>

Description

C # methods must add "[WebMethod]"

The foreground page must use a reference server control

<asp:scriptmanager id= "ScriptManager1" runat= "Server" enablepagemethods= "true" >
</asp:ScriptManager>

Calling method: Pagemethods. Background method Name (parameter [, parameter ...], method name called after success);

Ajax Library

2. jquery method

C # code:


[WebMethod]
public static string ABC (string ABC)
{
return ABC;
}

JS Code:

$ (). Ready (
function () {
$ ("#AjaxDemo"). Click (function () {
$.ajax ({
Type: "POST",
URL: "Default.aspx/abc",
Data: "{' ABC ': ' Test '}",
ContentType: "Application/json; Charset=utf-8 ",
Success:function (msg) {alert (msg);}
})
})
}
)

Description: The jquery library file must be referenced.

3. There is also a sort of reference to the AJAX.dll file. Register the foreground method in the background. This one seems to be used more in. net2.0. Specifically not studied carefully.



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.