AJAX.NET用户开发指南

来源:互联网  作者:pegger
摘要:概述AJAX依靠服务器作为中介来分发和处理请求。为了完成这项工作,.net封装类依赖于客户端的请求对象,而xmlHttpRequest对象被大部分的浏览器支持,因此使用这个对象是一个不错的解决方案。因为封装的目的是隐藏xmlHttpRequ……

其他工作方式

在其他类注册函数

在上面的例子及描述中,我们都是通过在页面的codebehind文件里完成函数的注册,但并不是说只能在页面的codebehide文件里完成注册,我们也可以在其他类中注册函数。回忆一下,Ajax封装类是通过在特定类里面查找那些有Ajax.AjaxMethod()属性的方法来完成工作的,这些类在客户端又通过两个script片断来完成返回值描述。使用Ajax.Utility.RegisterTypeForAjax,我们可以得到任何我们想得到类的详细内容。例如,下面的例子可以说明我们在其他类中使用服务器端函数是合法的:

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));
//...
}

记住,客户端调用使用这种格式的名字<ClassName>.<ServerSideFunctionName>。因此,如果上面的Serversideadd函数位于AjaxFunctions类,而不是页面类的话,客户端调用则变为:AjaxFunctions.ServerSideAdd(1,2)

代理是怎样工作的呢?

第二个script标签,如下面的示例

<script type="text/javascript" src="/cqyd/ajax/cqyd.SchemeSendWatch,cqyd.ashx"></script>

是由Ajax utility通过命名空间、类以及页面程序集自动生成的(当然也可以人工加入),从这一点我们可以想到Ajax.PageHandlerFactory是通过反射来取得有定制属性的函数的细节。很显然,Handler寻找带有AjaxMethod定制属性的函数,取得他们的特征(返回类型、名称、参数)并依据这些信息创建客户端代理。特别的,ajax创建一个与类型相同的JavaScript对象作为代理。

返回Unicode字符

Ajax.net可以从服务器端向客户端返回Unicode字符,为了做到这一点,在服务端函数返回时返回的值必须是Html编码的:

568>[Ajax.AjaxMethod]

public string Test1(string name, string email, string comment){
string html = "";
html += "Hello " + name + "<br>";
html += "Thank you for your comment <b>";
html += System.Web.HttpUtility.HtmlEncode(comment);
html += "</b>.";
return html;
}
  SessionState
  在服务端函数,你可能需要接受传送的session信息,为了做到这一点,必须要在想实现这个方式的服务端函数的Ajax.AjaxMethod属性上传递一个参数。

在查看ajax可以支持session的时候,我们先看看其他的特征。在下面这个例子中,我们有一个文档管理系统,当一个用户对文档进行编辑的时候会给这个文档加锁,其他用户需要等到这个文档可用时才能修改。不使用Ajax,用户需要不断等待刷新,因为不得不不断的去检查文档的状态是否为可用,这当然不是一个很好的方案。用ajax的session state支持,这就比较容易了。

我们首先写一个函数,这个函数通过遍历文档ID找到用户需要的文档,存储到session里,并返回没有占用的文档:

'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);
if (document != null && document.Status == DocumentStatus.Ready){
readyDocuments.Add(document);
}
}
return readyDocuments;
}
}

我们在属性参数中指明了HttpSessionStateRequirement.Read(还可以是Write and ReadWrite)

下面写javascript函数来使用这个方法带来的结果:

<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.DocumentId + "\">" + response.value.Name + "</a><br />";
}
}
setTimeout('page.DocumentReleased(DocumentsReady_CallBack)', 10000);
}

</script>
<body onload="setTimeout('Document.DocumentReleased(DocumentsReady_CallBack)', 10000);">

页面加载后每10秒钟向服务器函数请求一次。如果有返回,则call back函数检查response,并把最新的结果显示出来。

结论

Ajax技术可以给客户端提供丰富的客户体验,而ajax.net为您容易的实现这样强大的功能提供了可能。

【相关文章】好搜一下
C#中的delegate和event比较释义

C#中的delegate和event比较释义

在基于Windows平台的程序设计中,事件(event)是一个很重要的概念。因为…