Master's current project is ready to use Ajax and implement it with ajax.net instead of ATLAS. So I should first look at ajax.net. In this way, the master's car-based learning will be very efficient ~~, Of course, you must first learn:
The latest version of Ajax. NET 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>
<Httphandlers>
<Add verb = "post, get" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro.2"/>
</Httphandlers>
It means that all ajaxpro/*. ashx requests are composedAjax. pagehandlerfactory, instead of the defaultSystem. Web. UI. pagehandlerfactory Processing Program Factory.
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. Code As 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. There are detailed comments in the code. I think if a cainiao like me can understand it more deeply than I do.
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>
</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. The following one is if the server returns data
// The JS function name of the client to process the data. Its parameter is the data transmitted 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: