Ajax.Net Professional——A quick guide how to start(AjaxPro開始嚮導)

Because I could not write a documentation I will show you here how to start:

  • Download the latest Ajax.NET Professional files from www.schwarz-interactive.de
  • Add a reference to the AjaxPro.2.dll (for the .NET 1.1 Framework use AjaxPro.dll)
  • Add following lines to your web.config:

因爲我不會寫文檔,我將以如下方式告訴你如何開始:

(1)下載最新版的AjaxPro文件

(2)添加AjaxPro的引用。(.NET1.1使用AjaxPro.dll,其他版本的.NET使用AjaxPro.2.dll)

在web.config文件中添加以下幾行:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
    </httpHandlers>

  [...]

  </system.web>
</configuration>
ashx百度百科:http://baike.baidu.com/view/3799515.htm

PS:CSDN的編輯器有點問題,源代碼裏邊並沒有<strong>編輯,該標記是CSDN的代碼格式化編輯,在編輯頁面中沒有該標記,但在發佈後的頁面中卻多了該標記,要注意把它刪掉。

Now, you have to mark your .NET methods with an AjaxMethod attribute:

然後,你需要使用AjaxMethod屬性來標註你的.NET方法。

[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
  return DateTime.Now;
}
To use the .NET method on the client-side JavaScript you have to register the methods, this will be done to register a complete class to Ajax.NET:

在客戶端使用JS來調用.NET方法,你需要註冊這些方法(被JS調用的.NET方法)。通過註冊一個完整的類名來實現。

namespace MyDemo
{
  public class _Default
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
    }

    [AjaxPro.AjaxMethod]
    public DateTime GetServerTime()
    {
      return DateTime.Now;
    }
  }
}
ps:使用AjaxPro.Utility.RegisterTypeForAjax()方法來註冊一個類爲Ajax類。參數爲類名,可通過使用typeof操作符獲取。

  • If you start the web page two JavaScript includes are rendered to the HTML source.
  • 如果你的web頁面包含以下兩段JS代碼,在HTML源代碼中將會顯示爲下邊代碼所示:
  • To call a .NET method form the client-side JavaScript code you can use following syntax:
  • 在客戶端通過JS代碼來調用一個.NET(服務器端)方法,使用下邊所示的語法:
function getServerTime()
{
  MyDemo._Default.GetServerTime(getServerTime_callback);  // asynchronous call
}

// This method will be called after the method has been executed
// and the result has been sent to the client.

function getServerTime_callback(res)
{
  alert(res.value);
}
PS:使用完全的限定名,命名空間.類名.方法名:MyDemo._Default.GetServerTime在客戶端,使用JS代碼,方法getServerTime調用服務器端的GetServerTime方法來獲取時間,返回值可以直接賦值給客戶端JS的一個變量或者通過回調函數,將返回值發送給回調函數,作爲其參數,然後在回調函數中做處理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章