概述
ajax依靠服务器作为中介来分发和处理请求。为了完成这项工作,.net封装类依赖于客户端的请求对象,而xmlhttprequest对象被大部分的浏览器支持,因此使用这个对象是一个不错的解决方案。因为封装的目的是隐藏xmlhttprequest的实现,故我们不讨论他的实现细节。
封装类是通过在.net的方法上增加ajax属性标记来实现的,一旦被标记,ajax创建客户端的javascript函数(这类似于客户端编写的javascript函数),并使用xmlhttprequest创建服务器代理,这个代理映射客户端的函数到服务器的处理函数。
复杂吗?不会的,让我们看看下面的简单例子,给出的.net 函数
'vb.net public function add(firstnumber as integer, secondnumber as integer) as integer return firstnumber + secondnumber end sub |
//c# public int add(int firstnumber, int secondnumber) { return firstnumber + secondnumber; } |
如果你在安装引用时遇到了麻烦,可以参考这个链接的说明: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingremovingreferences.asp |
| <configuration> <system.web> <httphandlers> <add verb="post,get" path="ajax/*.ashx" type="ajax.pagehandlerfactory, ajax" /> </httphandlers> ... <system.web> </configuration> |
| 'vb.net public class index inherits system.web.ui.page private sub page_load(sender as object, e as eventargs) handles mybase.load ajax.utility.registertypeforajax(gettype(index)) '... end sub '... end class |
| //c# public class index : system.web.ui.page{ private void page_load(object sender, eventargs e){ ajax.utility.registertypeforajax(typeof(index)); //... } //... } |
| <script language="javascript" src="ajax/common.ashx"></script> <script language="javascript" src="ajax/namespace.pageclass,assemblyname.ashx"></script> |
namespace.pageclass | 当前页面的命名空间和类 |
assemblyname | 当前页面的程序集的名称 |
| <%@ page inherits="ajaxplay.sample" codebehind="sample.aspx.cs" ... %> <html> <head> <script language="javascript" src="ajax/common.ashx"></script> <script language="javascript" src="ajax/ajaxplay.sample,ajaxplay.ashx"></script> </head> <body> <form id="form1" method="post" runat="server"> ... </form> </body> </html> |
| 'vb.net <ajax.ajaxmethod()> _ public function serversideadd (byval firstnumber as integer, byval secondnumber as integer) as integer return firstnumber + secondnumber end function |
//c# [ajax.ajaxmethod()] public int serversideadd(int firstnumber, int secondnumber) { return firstnumber + secondnumber; } |
| <%@ page inherits="ajaxplay.sample" codebehind="sample.aspx.cs" ... %> <html> <head> <script language="javascript" src="ajax/common.ashx"></script> <script language="javascript" src="ajax/ajaxplay.sample,ajaxplay.ashx"></script> </head> <body> <form id="form1" method="post" runat="server"> <script language="javascript"> var response = sample.serversideadd(100,99); alert(response.value); </script> </form> </body> </html> |
| sample.serversideadd(100,99, serversideadd_callback); function serversideadd_callback(response){ if (response.error != null){ alert(response.error); return; } alert(response.value); } |
value | 服务器端函数执行的返回值(可能是一个字符串、自定义对象或者dataset) |
error | 如果发生错误,则返回错误信息. |
request | 原始的xmlhttprequest请求 |
context | 一个上下文对象 |
如果你想了解更多的关于xmlhttprequest的知识,可以查看下面的链接: http://www.quirksmode.org/blog/archives/2005/02/xmlhttp_linkdum.html |
| <script language="javascript"> //asynchronous call to the mythical "getdataset" server-side function function getdataset(){ ajaxfunctions.getdataset(getdataset_callback); } function getdataset_callback(response){ var ds = response.value; if(ds != null && typeof(ds) == "object" && ds.tables != null){ var s = new array(); s[s.length] = "<table border=1>"; for(var i=0; i<ds.tables[0].rows.length; i++){ s[s.length] = "<tr>"; s[s.length] = "<td>" + ds.tables[0].rows[i].firstname + "</td>"; s[s.length] = "<td>" + ds.tables[0].rows[i].birthday + "</td>"; s[s.length] = "</tr>"; } s[s.length] = "</table>"; tabledisplay.innerhtml = s.join(""); } else{ alert("error. [3001] " + response.request.responsetext); } } </script> |
| [serializable()] public class user{ private int _userid; private string _firstname; private string _lastname; public int userid{ get { return _userid; } } public string firstname{ get { return _firstname; } } public string lastname{ get { return _lastname; } } public user(int _userid, string _firstname, string _lastname){ this._userid = _userid; this._firstname = _firstname; this._lastname = _lastname; } public user(){} [ajaxmethod()] public static user getuser(int userid){ //replace this with a db hit or something :) return new user(userid,"michael", "schwarz"); } } |
| private void page_load(object sender, eventargs e){ utility.registertypeforajax(typeof(user)); } |
| <script language="javascript"> function getuser(userid){ user.getuser(getuser_callback); } function getuser_callback(response){ if (response != null && response.value != null){ var user = response.value; if (typeof(user) == "object"){ alert(user.firstname + " " + user.lastname); } } } getuser(1); </script> |
| public class ajaxfunctions <ajax.ajaxmethod()> _ public function validate(username as string, password as string) as boolean 'do something 'return something end function end class |
| 'vb.net private sub page_load(sender as object, e as eventargs) handles mybase.load ajax.utility.registertypeforajax(gettype(ajaxfunctions)) '... end sub |
| //c# private void page_load(object sender, eventargs e){ ajax.utility.registertypeforajax(typeof(ajaxfunctions)); //... } |
| [ajax.ajaxmethod] public string test1(string name, string email, string comment){ string html = ""; html += "hello " + name + " "; html += "thank you for your comment <b>"; html += system.web.httputility.htmlencode(comment); html += "</b>."; return html; } |
| 'vb.net <ajax.ajaxmethod(httpsessionstaterequirement.read)> _ public function documentreleased() as arraylist if httpcontext.current.session("documentswaiting") is nothing then return nothing end if dim readydocuments as new arraylist dim documents() as integer = ctype(httpcontext.current.session("documentswaiting"), integer()) for i as integer = 0 to documents.length - 1 dim document as document = document.getdocumentbyid(documents(i)) if not document is nothing andalso document.status = documentstatus.ready then readydocuments.add(document) end if next return readydocuments end function |
| //c# [ajax.ajaxmethod(httpsessionstaterequirement.read)] public arraylist documentreleased(){ if (httpcontext.current.session["documentswaiting"] == null){ return null; } arraylist readydocuments = new arraylist(); int[] documents = (int[])httpcontext.current.session["documentswaiting"]; for (int i = 0; i < documents.length; ++i){ document document = document.getdocumentbyid(documents[i]); if (document != null && document.status == documentstatus.ready){ readydocuments.add(document); } } return readydocuments; } } |
| <script language="javascript"> function documentsready_callback(response){ if (response.error != null){ alert(response.error); return; } if (response.value != null && response.value.length > 0){ var div = document.getelementbyid("status"); div.innerhtml = "the following documents are ready!<br />"; for (var i = 0; i < response.value.length; ++i){ div.innerhtml += "<a href=\"edit.aspx?documentid=" + response.value[i].documentid + "\">" + response.value[i].name + "</a><br />"; } } settimeout('page.documentreleased(documentsready_callback)', 10000); } </script> <body onload="settimeout('document.documentreleased(documentsready_callback)', 10000);"> |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器