關於Ajax.Net的快速入門

來源:互聯網
上載者:User
現在的項目準備用ajax,用ajax.net實現,而不是atlas,所以先看下ajax.net,Ajax.Net現在的最新版本是AjaxPro5.11.4.2,是:www.schwarz-interactive.de

  首先我們建立個項目,名字是AjaxPro,我用的是vs2005beta2版本。

  右擊網站名字點add reference添加對我們剛剛下載來的那個叫AjaxPro.2.dll的引用,如果你用的是vs2003,則添加對AjaxPro.dll的引用,然後我們在添加個web.config檔案(很鬱悶的是vs2005不再自動添加web.config檔案拉),修改web.config如下:

<system.web> <httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx"
type="AjaxPro.AjaxHandlerFactory,
AjaxPro.2"/> </httpHandlers>

  意思是所有的ajaxpro/*.ashx請求都由Ajax.PageHandlerFactory處理,而不是由預設的System.Web.UI.PageHandlerFactory處理常式工廠來處理。

  我們現在給Default.aspx.cs檔案添加個名字空間namespace MyDemo,這裡更加鬱悶的是為什麼vs2005beta2怎麼不給你自動添加名字空間啊?和2003怎麼完全不同呢?

  現在我們寫個AjaxMethod伺服器端方法,他和普通的伺服器方法唯一不同的地方就是他必須要在方法的上面添加個[AjaxPro.AjaxMethod],代碼如下:

[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}
[AjaxPro.AjaxMethod]public int
AddTwo(int firstInt, int secondInt)
{
return firstInt + secondInt;
}

  我們還必須在Page_Load裡面把這個類註冊下,如下:

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

這個時候我們還必須修改aspx頁面的指令行,因為我們在後台搞了個名字空間,如下:Inherits="MyDemo._Default"也就是要把名字空間也寫上。我們再寫用戶端指令碼來調用伺服器方法。代碼裡有詳細的注釋,前台Default.aspx代碼:

<%@ Page Language="C#"
AutoEventWireup="true"CodeFile="Default.aspx.cs"
Inherits="MyDemo._Default" %>
<!DOCTYPE html PUBLIC "-
//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Untitled Page
</title></head><body><form id="form1"
runat="server"><div><input id="Button1"
type="button" value="獲得伺服器時間"
onclick="getServerTime();" /><input
id="Text1" type="text" /><input
id="Text2" type="text" /><input
id="Button2" type="button"
value="得到兩個文字框的和"
onclick="add(document.getElementById('Text1').
value,document.getElementById('Text2').value)"
/></div></form><script
type="text/javascript">
function getServerTime()
{
//MyDemo._Default.GetServerTime()
得到從伺服器傳來的資料是object,
要寫.valuealert
(MyDemo._Default.GetServerTime().value);
}
function add(a,b)
{
//把文字框的值轉換成intvar
a1 = parseInt(a);var b1 = parseInt(b);
//第1、2參數為伺服器方法所需要的參數,
後面一個是如果伺服器返回資料
//用戶端要處理這些資料的js函數名,
他有個參數就是從伺服器傳來的資料
MyDemo._Default.AddTwo(a1,b1,getAdd);
}
function getAdd(rel)
{
//要加上.valuealert(rel.value);
}
</script></body></html>
後台Default.aspx.cs代碼:
using System;using System.Data;
using System.Configuration;
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;
namespace MyDemo
{
public partial class _Default :
System.Web.UI.Page{protected void
Page_Load(object sender, EventArgs e){AjaxPro.Utility.RegisterTypeForAjax
(typeof(_Default));
}
[AjaxPro.AjaxMethod]public
DateTime GetServerTime()
{return DateTime.Now;
}[AjaxPro.AjaxMethod]public int AddTwo
(int firstInt, int secondInt)
{
return firstInt + secondInt;
}}}

按F5運行結果如下,
firefox裡面測試通過:

using System;using System.Data;
using System.Configuration;
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;
namespace MyDemo
{
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load
(object sender, EventArgs e)
{AjaxPro.Utility.RegisterTypeForAjax
(typeof(_Default));}
[AjaxPro.AjaxMethod]public DateTime
GetServerTime(){return DateTime.Now;
}
[AjaxPro.AjaxMethod]public
int AddTwo(int firstInt,
int secondInt)
{return firstInt + secondInt;}}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.