Microsoft ASP. NET Ajax: Use ajax to directly call the background page class Method

Source: Internet
Author: User

I have been using ajaxpro for Refresh. Today I use Microsoft's refresh framework. I found that many people use web services to call the backend Cs method. after searching on the internet, I found a method to directly call the CS class on the page.

It is hereby recorded for future use.

First, use vs. net2005 to create an ASP. NET Ajax-enabled web application.

1. Use ajax to directly call the background method:

BackgroundCode:

Namespace ajax1
{
Public partial class _ default: system. Web. UI. Page
{
// The webmethod must be identified.
[System. Web. Services. webmethod]
// Note that the front-end method must be public and static.
Public static string Hello (string name)
{
Return "Hello:" + name;
}
}
}
 
Front-end code:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "ajax1. _ 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> untitled page </title>
<SCRIPT type = "text/JavaScript">
Function btnclick (){
// Call the page background method, the parameters required by the preceding method, and the JS function to be executed when the method callback is successful. The last one is the JS function to be executed when the method callback fails.
Pagemethods. Hello ("you", funready, funerror );
}
// Result is the data returned by the background method.
Function funready (result ){
Alert (result );
}
// Err is the error message returned by the background method.
Function funerror (ERR ){
Alert ("error:" + err. _ message );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
You must add the enablepagemethods = "true" attribute to use the background method.
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true">
</ASP: scriptmanager>
<Input type = "button" onclick = "btnclick ();" value = "test"/>
</Div>
</Form>
</Body>
</Html>

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.