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. the config file is depressing because vs2005 does not automatically add web. config file), modify the web. config:
This means that all ajaxpro/*. ashx requests are handled by Ajax. PageHandlerFactory, rather than by the default System. Web. UI. PageHandlerFactory handler 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 let's write an AjaxMethod server method. The only difference between it and the common server method is that it must add a [AjaxPro. AjaxMethod] on the method. The Code is 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. The Code contains detailed annotations. The front-end Default. aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile = "Default. aspx. cs" Inherits = "MyDemo. _ Default" %>Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>
Untitled Page
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; } } }
|
(