使用AjaxPro.Net架構實現在用戶端調用服務端的方法_實用技巧

來源:互聯網
上載者:User

此文檔將使用AjaxPro.Net架構實現Ajax功能:在用戶端非同步呼叫服務端方法。AjaxPro.Net是一個優秀的.net環境下的Ajax架構,用法很簡單,可以查閱相關資料,本文檔是一個簡單的執行個體講述使用AjaxPro的幾個關鍵點。

1、下載AjaxPro 組件。並將AjaxPro.dll引用到網站(或項目)。下載:Download latest version 7.7.31.1.
2、修改Web.config。在 <system.web> 元素中添加以下代碼。
  <configuration><system.web> <httpHandlers> <!-- 註冊 ajax handler,2.0以上架構用AjaxPro.2 -->
    <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
  </httpHandlers> </system.web> </configuration>
3、對AjaxPro在頁Page_Load事件中進行運行時註冊。如:
  //AjaxPro.Utility.RegisterTypeForAjax(typeof(所在類的類名));類的類名。如是放在命名空間,則需要寫上完整的命名空間(如:namespaces._Default)
AjaxPro.Utility.RegisterTypeForAjax(typeof(testPro1));
4、建立伺服器端方法。只要給一個方法加上[AjaxPro.AjaxMethod]標記,該方法就變成一個AjaxPro可進行影射調用的方法。如下:(我現在是建立一個testPro1.aspx頁面,在它的cs代碼中加入)

複製代碼 代碼如下:

[AjaxPro.AjaxMethod]
public string GetString()
{
return "Hello AjaxPro";
}
[AjaxPro.AjaxMethod]
public string GetServerTime()
{
return DateTime.Now.ToString();
}

5、用戶端調用:
複製代碼 代碼如下:

<script type="text/javascript">
function getTime() {
alert(testPro1.GetServerTime().value);
}
function getServerStr() {
//ajaxPro_guide.GetString(GetString_callback); // asynchronous call
//var p = ClassPro.GetServerTime().toString();
alert(testPro1.GetString().value);
}
</script>

頁面中加入以下代碼:
    <input id="Button1" type="button" value="獲是伺服器時間" onclick="getTime()" />
    <input id="Button3" type="button" value="獲是伺服器對象" onclick="getStudent()" />

二、擴充,Client Access Server對象
  1、在App_code中建立類:
複製代碼 代碼如下:

public class Student
{
private string _name = "鄭伯城";
public int Age = 30;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
}

2、在測試頁面testPro1.aspx頁面,在它的cs代碼中加入
複製代碼 代碼如下:

[AjaxPro.AjaxMethod]
public Student GetStudent()
{//服務端添加GetStudent方法
return new Student();
}
private Student student = null;
[AjaxPro.AjaxMethod]
public void SetStudent(Student stu)
{
this.student = stu;
string name = this.student.Name;
}

3、aspx頁面的javascript指令碼
測試aspx頁面中的指令碼
複製代碼 代碼如下:

<head id="Head1" runat="server">
<title>ajaxPro測試</title>
<script type="text/javascript">
function getStudent() {
var stu = testPro1.GetStudent().value;
alert(stu.Name + " " + stu.Age); //客戶js可以訪問服務端返回的對象
}
function putStudent() {
var stu = testPro1.GetStudent().value;
stu.Name = "劉寧";
testPro1.SetStudent(stu); //客戶提交對象,並且對象的Name欄位已經改變為“劉寧”了。
alert(stu.Name + " " + stu.Age); //客戶js可以訪問服務端返回的對象
}
</script>
</head>

<div><input id="Button3" type="button" value="獲是伺服器對象" onclick="getStudent()" />
<input id="Button4" type="button" value="用戶端提交對象給伺服器" onclick="putStudent()" />
</div>
參考:官網

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.