Now the project is ready to use Ajax, with ajax.net implementation, rather than atlas, so first look at ajax.net, Ajax. Net now the latest version is ajaxpro5.11.4.2, is: www.schwarz-interactive.de
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>
<Add verb = "post, get" Path = "ajaxpro/*. ashx"
Type = "ajaxpro. ajaxhandlerfactory,
Ajaxpro.2 "/>
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 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 runat = "server"> <title> untitled page
</Title>
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"
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,
To write. valuealert
(Mydemo. _ default. getservertime (). value );
}
Function add (A, B)
{
// Convert the text box value to intvar
A1 = parseint (a); var b1 = parseint (B );
// Parameters 1st and 2 are required by the server method,
If the server returns data
// The Name Of The JS function that the client needs to process the data,
He has a parameter that is the data sent from the server.
Mydemo. _ default. addtwo (A1, B1, getadd );
}
Function getadd (rel)
{
// Add. valuealert (Rel. value );
}
</SCRIPT> </body>
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 following results,
Test passed 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 ;}}}