您的位置:首页 > 其它

AjaxPro组件的各种基本用法

2011-05-08 21:36 399 查看
前几天逛博客园,看到有朋友希望了解一下AjaxPro.2的用法。因为我之前在项目中有大量的使用这个组件,所以今天把AjaxPro的各种用法介绍一下,希望对有需要的朋友有所帮助,这将是我的荣幸!

一。AjaxPro的简单介绍

AjaxPro是.NET平台下的一个回调式AJAX框架。使用很简单,功能强大,能很容易实现各种Ajax功能。

二。AjaxPro的各种具体用法

我们通过一个Demo来介绍。下面是这个Demo的结构截图。

View Code

using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

namespace AjaxProDemo
{
/// <summary>
///data 的摘要说明
/// </summary>
public class data
{
public data()
{

}
/// <summary>
/// 无参数方法
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string GetString()
{
return "Hello,AjaxPro";
}

/// <summary>
/// 简单的带参数方法
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string GetStringByParam(string name)
{
return name;
}

/// <summary>
/// 返回简单的集合
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public List<string> GetStringList()
{
List<string> result = new List<string>();
result.Add("hello");
result.Add("abc");
result.Add("aaaaa");
return result;
}

/// <summary>
/// 返回实体对象
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public People GetObject()
{
People people = new People();
people.Name = "自由梦想";
people.Age = 23;
people.Sex = "男";
people.Address = "广东深圳";
return people;
}

/// <summary>
/// 返回实体对象集合
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public List<People> GetObjectList()
{
List<People> result = new List<People>();
People people = new People();
people.Name = "自由梦想";
people.Age = 23;
people.Sex = "男";
people.Address = "广东深圳";
result.Add(people);
People people2 = new People();
people2.Name = "张强";
people2.Age = 23;
people2.Sex = "男";
people2.Address = "广东深圳";
result.Add(people2);
return result;
}
/// <summary>
/// 传递对象参数
/// </summary>
/// <param name="people"></param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public People GetObjectByObjParam(People people)
{
return people;
}
}

public class People
{
private string _Name;

public string Name
{
get { return _Name; }
set { _Name = value; }
}
private int _Age;

public int Age
{
get { return _Age; }
set { _Age = value; }
}
private string _Sex;

public string Sex
{
get { return _Sex; }
set { _Sex = value; }
}
private string _Address;

public string Address
{
get { return _Address; }
set { _Address = value; }
}
}
}


AjaxMethod:注册某个方法为Ajax方法,只有注册了这个属性,方法才能在客户端被调用。

在客户端调用之前,必须先注册要调用的类,注册方法如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxProDemo;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(data));
AjaxPro.Utility.RegisterTypeForAjax(typeof(People));
}
}


这里有一点应该注意:应该在Page_Load中注册类,并且必须在IsPostBack外面注册。如果放在IsPostBack里面注册,当页面回传后将会出错。

a)调用无参数的函数

function doAjax(){
AjaxProDemo.data.GetString(callBack);   //callBack为回调函数
}
function callBack(ret) {
alert(ret.value);         //通过value获得返回值
}


客户端调用时,必须要用类的全名:命名空间+类名

b)调用简单的带参数函数

function doAjax() {
AjaxProDemo.data.GetStringByParam("Hello World!", callBack);//先传参数,然后指定回调函数.
}

function callBack(ret) {
alert(ret.value);
}


c)调用返回简单集合的方法

function doAjax() {
AjaxProDemo.data.GetStringList(callBackList);
}

function callBackList(ret) {
var list = ret.value;
for (var i = 0; i < list.length; i++) {
alert(list[i]);
}
}


返回数组应该是一样,我没有试过,感兴趣的朋友可以自己尝试。

d)调用返回实体对象的函数

function doAjax() {
AjaxProDemo.data.GetObject(callBackObj);
}
function callBackObj(ret) {
var people = ret.value;
alert(people.Name + "|" + people.Age + "|" + people.Sex + "|" + people.Address);
}


哇!是不是很爽啊,直接用返回值去访问属性就OK了。很强大吧!?不过别忘了要现在Page_Load事件里面注册People类。

e)调用返回返回实体对象集合的函数

function doAjax() {
AjaxProDemo.data.GetObjectList(callBackObjList);
}
function callBackObjList(ret) {
var peopleList = ret.value;
for (var i = 0; i < peopleList.length; i++) {
var people = peopleList[i];
alert(people.Name + "|" + people.Age + "|" + people.Sex + "|" + people.Address);
}
}


f)调用以实体对象为参数的函数

function doAjax() {
var people = new Object();
people.Name = "自由梦想";
people.Age = 23;
people.Address = "广东深圳";
people.Sex = "男";
AjaxProDemo.data.GetObjectByObjParam(people, callBackObj);
}
function callBackObj(ret) {
var people = ret.value;
alert(people.Name + "|" + people.Age + "|" + people.Sex + "|" + people.Address);
}


以上就是AjaxPro的一些基本应用,希望本文对有需要的朋友有用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: