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>