返回到目录:晒晒我的Ajax服务端框架

此功能将让您在Javascript直接调用当前aspx页面中的一个C#方法。示例代码如下:

C#方法

public partial class Customers : MyBasePage
{
    /// <summary>
    /// 供AJAX调用:更新客户资料。具体调用方法请参考JS代码。
    /// 方法可以是静态的,也可以是不是静态的。
    /// </summary>
    /// <param name="customer"></param>
    /// <returns></returns>
    public static int UpdateCustomerInfo(Customer customer)
    {
        customer.EnsureItemIsOK();

        return BllFactory.GetCustomerBLL().Update(customer);
    }

}

public sealed class Customer : MyDataItem
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string Tel { get; set; }
}

Javascript调用代码

$.ajax({
    type: "POST", 
    // 注意参数:AjaxPageMethod,它指出了要调用页面的哪个方法。
    data: $.param({ AjaxPageMethod: "UpdateCustomerInfo", CustomerID: customerId }) + "&" 
			+ $("#divCustomerInfo :text").fieldSerialize(),
    //......
});

以上功能还需要以下配置:

<httpModules>
    <clear/>
    <add name="PageMethodModule" type="FishWebLib.Ajax.PageMethodModule, FishWebLib, 
			Version=3.0.0.0, Culture=neutral, PublicKeyToken=04db02423b9ebbb2" />
</httpModules>

当上面的JS在运行时,将会把当前页面以AJAX方式提交到当前Customers.aspx页面的UpdateCustomerInfo方法中。

好了,这个演示就写到这里,更多细节请查看用户手册。

返回到目录:晒晒我的Ajax服务端框架

点击此处进入示例展示及下载页面

posted on 2011-03-13 12:12  Fish Li  阅读(4407)  评论(4编辑  收藏  举报