ajax
這是根據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;
}
}
}