使用AjaxPro.Net框架實(shí)現(xiàn)在客戶端調(diào)用服務(wù)端的方法
此文檔將使用AjaxPro.Net框架實(shí)現(xiàn)Ajax功能:在客戶端異步調(diào)用服務(wù)端方法。AjaxPro.Net是一個(gè)優(yōu)秀的.net環(huán)境下的Ajax框架,用法很簡(jiǎn)單,可以查閱相關(guān)資料,本文檔是一個(gè)簡(jiǎn)單的實(shí)例講述使用AjaxPro的幾個(gè)關(guān)鍵點(diǎn)。
1、下載AjaxPro 組件。并將AjaxPro.dll引用到網(wǎng)站(或項(xiàng)目)。下載:Download latest version 7.7.31.1.
2、修改Web.config。在 <system.web> 元素中添加以下代碼。
<configuration><system.web> <httpHandlers> <!-- 注冊(cè) ajax handler,2.0以上框架用AjaxPro.2 -->
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
</httpHandlers> </system.web> </configuration>
3、對(duì)AjaxPro在頁P(yáng)age_Load事件中進(jìn)行運(yùn)行時(shí)注冊(cè)。如:
//AjaxPro.Utility.RegisterTypeForAjax(typeof(所在類的類名));類的類名。如是放在命名空間,則需要寫上完整的命名空間(如:namespaces._Default)
AjaxPro.Utility.RegisterTypeForAjax(typeof(testPro1));
4、創(chuàng)建服務(wù)器端方法。只要給一個(gè)方法加上[AjaxPro.AjaxMethod]標(biāo)記,該方法就變成一個(gè)AjaxPro可進(jìn)行影射調(diào)用的方法。如下:(我現(xiàn)在是新建一個(gè)testPro1.aspx頁面,在它的cs代碼中加入)
[AjaxPro.AjaxMethod]
public string GetString()
{
return "Hello AjaxPro";
}
[AjaxPro.AjaxMethod]
public string GetServerTime()
{
return DateTime.Now.ToString();
}
5、客戶端調(diào)用:
<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="獲是服務(wù)器時(shí)間" onclick="getTime()" />
<input id="Button3" type="button" value="獲是服務(wù)器對(duì)象" onclick="getStudent()" />
二、擴(kuò)展,客戶端訪問服務(wù)器對(duì)象
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、在測(cè)試頁面testPro1.aspx頁面,在它的cs代碼中加入
[AjaxPro.AjaxMethod]
public Student GetStudent()
{//服務(wù)端添加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腳本
測(cè)試aspx頁面中的腳本
<head id="Head1" runat="server">
<title>ajaxPro測(cè)試</title>
<script type="text/javascript">
function getStudent() {
var stu = testPro1.GetStudent().value;
alert(stu.Name + " " + stu.Age); //客戶js可以訪問服務(wù)端返回的對(duì)象
}
function putStudent() {
var stu = testPro1.GetStudent().value;
stu.Name = "劉寧";
testPro1.SetStudent(stu); //客戶提交對(duì)象,并且對(duì)象的Name字段已經(jīng)改變?yōu)椤皠帯绷恕?
alert(stu.Name + " " + stu.Age); //客戶js可以訪問服務(wù)端返回的對(duì)象
}
</script>
</head>
<div><input id="Button3" type="button" value="獲是服務(wù)器對(duì)象" onclick="getStudent()" />
<input id="Button4" type="button" value="客戶端提交對(duì)象給服務(wù)器" onclick="putStudent()" />
</div>
參考:官網(wǎng)
- 完美解決Could not load file or assembly AjaxPro.2 or one of its dependencies. 拒絕訪問。
- ajaxpro.dll 控件實(shí)現(xiàn)異步刷新頁面
- asp.net下使用AjaxPro實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)代碼
- jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax輔助方法
- 分享AjaxPro或者Ajax實(shí)現(xiàn)機(jī)制
- 關(guān)于服務(wù)器或虛擬主機(jī)不支持 AjaxPro 的問題終極解決方法
- 編寫輕量ajax組件02--淺析AjaxPro
相關(guān)文章
asp.net下使用jQuery.AutoComplete完成仿淘寶商品搜索自動(dòng)完成功能(改進(jìn)了鍵盤上下選擇體驗(yàn))
其實(shí)這個(gè)已經(jīng)是個(gè)比較常見的功能了,網(wǎng)上也有很多人做過這個(gè)了,但是很多都是僅僅做了一些基本的網(wǎng)頁上自動(dòng)完成功能,沒有與具體的數(shù)據(jù)庫進(jìn)行聯(lián)動(dòng),我今天所介紹這個(gè)自動(dòng)完成的就是我修改的jQuery.AutoComplete+數(shù)據(jù)庫的一個(gè)解決方案。2010-05-05
asp.net利用NamingContainer屬性獲取GridView行號(hào)的方法
在最近的一個(gè)項(xiàng)目中,用到在GridView模板列中添加有DropDownList控件,并開啟其AutoPostback屬性。當(dāng)發(fā)生SelectedIndexChanged事件時(shí),想同時(shí)獲取其所在的行號(hào),從而獲取相應(yīng)的行信息。2013-07-07
代碼實(shí)現(xiàn)打印功能(asp.net+javascript)
頁面實(shí)現(xiàn)打印的效果代碼,分為服務(wù)器端和客戶端單個(gè)即可,客戶端的比較不錯(cuò),本站也是類似的方法。2009-05-05
在Asp.net網(wǎng)頁上寫讀Cookie的兩種不同語法介紹
asp.net開發(fā)時(shí),為了存儲(chǔ)一些信息通常是Session與Cookie同時(shí)使用,本文將會(huì)補(bǔ)充一下Cookie相關(guān)的資料,感興趣的朋友可以了解一下在網(wǎng)頁上寫讀Cookie的實(shí)現(xiàn),希望本文對(duì)你有所幫助2013-01-01

