一个简单的AjaxPro使用案例

准备工作:

1.登录 http://www.ajaxpro.info/ 下载AjaxPro;
2.在项目中添加对AjaxPro.2.dll的引用;
3.在Web.config中增加location节点:
<configuration>
    <location path="ajaxpro">
      <system.web>
        <httpHandlers>
          <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
        </httpHandlers>
      </system.web>
    </location>
</configuration>


使用案例:

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="MyDIV"></div>
    <input id="Button1" type="button" onclick="_Default.GetValueA('mzwu.com',callback);" value="GetValueA" />
    <input id="Button2" type="button" onclick="_Default.GetValueB('mzwu.com',callback);"  value="GetValueB" />
    <script type="text/javascript">
    function callback(val)
    {
        document.getElementById("MyDIV").innerHTML = val.value;
    }
    </script>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //注册AjaxPro,括号中的参数是当前的类名
        AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));

        //标记,查看页面是否刷新
        Response.Write(DateTime.Now);
    }

    /// <summary>
    /// 在字符串前加AAA_
    /// </summary>
    /// <param name="strTemp"></param>
    /// <remarks>具有 AjaxPro.AjaxMethod 特性</remarks>
    /// <returns></returns>
    [AjaxPro.AjaxMethod]
    public string GetValueA(string strTemp)
    {
        return "AAA_" + strTemp;
    }

    /// <summary>
    /// 在字符串前加BBB_
    /// </summary>
    /// <param name="strTemp"></param>
    /// <remarks>具有 AjaxPro.AjaxMethod 特性</remarks>
    /// <returns></returns>
    [AjaxPro.AjaxMethod]
    public string GetValueB(string strTemp)
    {
        return "BBB_" + strTemp;
    }
}

说明:

1.必须在Page_Load注册AjaxPro;
2.AjaxPro的方法必须在前边加[AjaxPro.AjaxMethod],且访问级别一定要是public;
3.客户端使用"类名.方法名(参数列表,回调函数)"的形式直接调用服务器端函数;

评论: 0 | 引用: 0 | 查看次数: 4051
发表评论
登录后再发表评论!