First, create a new project named ajaxpro. I use vs2005beta2.
Right-click the site name and click Add reference to add a reference to the ajaxpro.2.dll we just downloaded. If vs2003 is used, add the reference to ajaxpro. DLL reference, and then we add a web. config File (it is depressing that vs2005 will not automatically add the web. config file), modify the web. config:
<System. web>
This means that all ajaxpro/*. ashx requests are processed by Ajax. pagehandlerfactory, rather than by the default system. Web. UI. pagehandlerfactory.ProgramFactory.
Now we can add a namespace mydemo to the default. aspx. CS file. What is even more depressing here is why does vs2005beta2 not automatically add a namespace to you? How is it totally different from 2003?
Now we write an ajaxmethod server method. The only difference between this method and the common server method is that it must add a [ajaxpro. ajaxmethod] on the method.CodeAs follows:
[Ajaxpro. ajaxmethod]
Public datetime getservertime ()
{Return datetime. Now ;}
[Ajaxpro. ajaxmethod]
Public int addtwo (INT firstint, int secondint)
{Return firstint + secondint ;}
We must register this class in page_load as follows:
Protected void page_load (Object sender, eventargs E)
{Ajaxpro. Utility. registertypeforajax (typeof (_ default ));}
At this time, we must modify the <% @ page command line of the ASPX page, because we have a namespace in the background, as shown below: inherits = "mydemo. _ default "is to write the namespace. We will then write the client script to call the server method. The Code contains detailed annotations. The front-end default. aspx code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "mydemo. _ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml"> </Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "button1" type = "button" value = "Get server time" onclick = "getservertime ();"/>
<Input id = "text1" type = "text"/>
<Input id = "text2" type = "text"/>
<Input id = "button2" type = "button" value = "Get the sum of the two text boxes and" onclick = "add (document. getelementbyid ('text1 '). value, document. getelementbyid ('text2 '). value) "/>
</Div>
</Form>
<SCRIPT type = "text/JavaScript">
Function getservertime ()
{// Mydemo. _ default. getservertime (). The data sent from the server is an object, which must be written as. value.
Alert (mydemo. _ default. getservertime (). value );
}
Function add (A, B)
{// Convert the text box value to int
VaR a1 = parseint ();
VaR b1 = parseint (B );
// Parameters 1st and 2 are the parameters required by the server method, followed by the JS function name if the server returns data // The client processes the data, he has a parameter that is the data sent from the server.
Mydemo. _ default. addtwo (A1, B1, getadd );
}
Function getadd (rel)
{// Add. Value
Alert (Rel. value );
}
</SCRIPT>
</Body>
</Html>
Background default. aspx. CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Namespace mydemo
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
}
[Ajaxpro. ajaxmethod]
Public datetime getservertime ()
{Return datetime. Now ;}
[Ajaxpro. ajaxmethod]
Public int addtwo (INT firstint, int secondint)
{Return firstint + secondint ;}
}
}
Press F5 to run the result as follows, and the test passes in Firefox:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Namespace mydemo
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
}
[Ajaxpro. ajaxmethod]
Public datetime getservertime ()
{Return datetime. Now ;}
[Ajaxpro. ajaxmethod]
Public int addtwo (INT firstint, int secondint)
{Return firstint + secondint ;}}}