这是根据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="返回个类对象" οnclick="getClass()" /><br/>
<input type=button value="返回个继承类对象" οnclick="getInheritedClass();" /><br/>
<input type=button value="提交个类对象给服务器端" οnclick="putClass();"/>
</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;
}
}
}