Javascript call Asp.net post code method

Source: Internet
Author: User

Aspx:

 <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "pricesystem_temp_default" %> 

<! Doctype Html Public "-// W3C // dtd xhtml 1.0 transitional // en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > </ Title >
</ Head >

< Script SRC = "../JS/jquery-1.5.1.min.js" Type = "Text/JavaScript" > </ Script >
 
1:

2:

 
3:<SCRIPT>

 
4:$ (Function(){

5:Pagemethods. Hello ("James",Function(E ){

6:Alert (E );

 
7:});

 
8:})

</Script>

< Body >
< Form ID = "Form1" Runat = "Server" >
< ASP: scriptmanager ID = "Scriptmanager1" Enablepagemethods = "True" Runat = "Server" > </ ASP: scriptmanager >
< Div >

</Div>
</Form>
</Body>
</Html>

 

CS

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;

Using System. xml;
Using System. xml. XPath;
Using System. xml. LINQ;
Using System. Data;
Using System. Web. Services;

Public Partial Class Pricesystem_temp_default: pagebase
{
Protected Void Page_load ( Object Sender, eventargs E)
{

}

[Webmethod (enablesession = True )]
Public Static String Hello ( String Name)
{
Return "Hello ," + Name;
}
}

 

 
Summary ASP. net Ajax asynchronously calls the Web service on the server in the client JavaScript. We need to: 1 to add the [scriptservice] attribute for the Web service class or the web service method to be exposed to the client; 2. Add the [webmethod] attribute to the method exposed to the client in the Web service; 3. Add a reference to the Web service in the scriptmanager control on the page; 4. Use the following JavaScript syntax on the client to call the Web Service: [namespace]. [classname]. [methodname] (param1, param2 ,..., callbackfunction) 5. Specify a callback function for the client to call asynchronously, and receive and process the returned value in the callback function. Use ASP. net Ajax asynchronous calls in client JavaScript are defined in ASP. NET page, we need to: 1 declare this method as public; 2 declare this method as class method (static, VB in C.. 3. Add the [webmethod] attribute to the method. 4. Set the enablepagemethods attribute of the scriptmanager control on the page to true; 5. Use the following JavaScript syntax on the client to call the PAGE method: pagemethods. [methodname] (param1, param2 ,..., callbackfunction); 6. Specify the callback function for the client asynchronous call, and receive and process the return value in the callback function.

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.