這是根據AJax.Net作者寫的Demo做的,大概意思就是能夠從客戶端提交個服務器端定義的類給服務器端,服務器端也能夠返回個Class給客戶端,我簡化了作者的代碼,在ASPx頁面中沒有使用cs文件,同時把所有的服務器端方法都放到了一個cs文件中!代碼中賦有注釋,我發覺這個AJax框架真的很簡單的,好象現在看起來他比atlas和無刷新回調都要來的簡單啊 ,所有代碼在IE6和Firefox下通過
前台ClassTest.ASPx代碼如下:
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xHtml11.dtd">
<script runat=server>
void Page_Load(object sender, EventArgs e)
{
//我們要把名字空間MyDemo下的Demo類注冊下
AjaxPro.Utility.RegisterTypeForAJax(typeof(MyDemo.Demo));
}
</script>
<Html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type=button value="返回個類對象" /><br/>
<input type=button value="返回個繼承類對象" /><br/>
<input type=button value="提交個類對象給服務器端" />
</div>
</form>
<script type="text/Javascript">
function getClass()
{
MyDemo.Demo.GetMyClass(getMyClass);
}
function getMyClass(ret)
{
var a = ret.value;
alert(a.FirstName);
}
function getInheritedClass()
{
MyDemo.Demo.GetInheritedClass(getInhClass);
}
function getInhClass(tg)
{
var a = tg.value;
alert(a.LastName+"\n"+a.gid);
}
function putClass()
{
var p = MyDemo.Demo.GetMyClass().value;
//先調用服務器端方法GetMyClass返回個MyClass對象
p.FirstName = "老鼠是";
//第一個參數為服務器端方法所需要的參數,第二個為接受數據處理的JS函數
MyDemo.Demo.PutClass(p,putMyClass);
}
function putMyClass(dsf)
{
var a = dsf.value;//得到服務器傳來的數據
alert(a.FirstName+"\n"+a.LastName+"的徒弟!");
}
</script>
</body>
</Html>
MyClass.cs 文件代碼:只有被客戶端調用的方法才要在方法上面加[AjaxPro.AJaxMethod]
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;
using AJaxPro;
/// <summary>
/// Summary description for MyClass
/// </summary>
///
namespace MyDemo
{
public class MyClass
{
public MyClass()
{
//
// TODO: Add constructor logic here
//
}
public string FirstName = "";
public string LastName = "";
public int Age = 0;
}
//下面這個類繼承自MyClass類,他將具有和上面類一樣的公共屬性
public class MyInheritedClass : MyClass
{
public double Size = 0.0;
public Guid gid = Guid.Empty;
}
public class Demo
{
public Demo(){}
[AjaxPro.AJaxMethod]
public MyClass GetMyClass()
{
//這個方法將返回一個MyClass類的實例
MyClass c = new MyClass();
c.FirstName = "老鼠";
c.LastName = "吳旗娃";
c.Age = 30;
return c;
}
//這個方法將返回給客戶端一個繼承類MyInheritedClass類的實例
[AjaxPro.AJaxMethod]
public MyInheritedClass GetInheritedClass()
{
//初始化個MyInheritedClass類對象,因為繼承,所以他也具有父類公共屬性
MyInheritedClass c = new MyInheritedClass();
c.FirstName = "老鼠";
c.LastName = "吳旗娃";
c.Age = 30;
c.Size = 4.5;
c.gid = Guid.NewGuid();
return c;
}
//下面這個方法獲得從客戶端傳來的數據(類對象)再返回一個類對象給客戶端
[AjaxPro.AJaxMethod]
public MyClass PutClass(MyClass c)
{
c.LastName = "吳旗娃師傅";
return c;
}
}
}