| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#E1B][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#E1B]简介[/url] |
| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#E5B][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#E5B]什么是 AJAX?[/url] |
| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#EWC][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#EWC]用于 ASP.NET 的 AJAX[/url] |
| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#ERD][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#ERD]手头的 AJAX[/url] |
| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#ELGAC][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#ELGAC]AJAX 与您[/url] |
| [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#EUGAC][img]http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif[/img] [/url] | [url=http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPNetSpicedAjax.mspx#EUGAC]结论[/url] |
<configuration> <system.web> <httpHandlers> <!-- Register the ajax handler --> <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" /> </httpHandlers> ... ... </system.web> </configuration>为了使服务器端函数在 JavaScript 中可用,必须做两件事情。首先,要使用的函数必须标有 [b]Ajax.AjaxMethodAttribute[/b]。其次,在页加载事件期间,必须通过调用 Ajax[b].Utility.RegisterTypeForAjax[/b] 来注册包含这些函数的类。听起来似乎有些复杂,但请不必担心;实际上只需要在代码中多加两行。让我们看一个示例。
//C#
public class Sample :System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//注册我们感兴趣的包含服务器端函数
//的类
Ajax.Utility.RegisterTypeForAjax(typeof(Sample));
}
[Ajax.AjaxMethod()]
public string GetMessageOfTheDay()
{
return "Experience is the mother of wisdom";
}
}
'VB.NET
Public Class Sample
Inherits System.Web.UI.Page
Private Sub Page_Load(sender AsObject, e As EventArgs)
Handles MyBase.Load
'注册我们感兴趣的包含服务器端函数
'的类
Ajax.Utility.RegisterTypeForAjax(GetType(Sample))
End Sub
<Ajax.AjaxMethod()> _
Public Function GetMessageOfTheDay() As String
Return "Experience is the mother of wisdom"
End Function
End Class
以上示例首先告知 Ajax.NET 在 Sample 类中查找友好的 Ajax 方法。它正好是与实际页相同的类,但是它可以是任意 .NET 类,或可以注册多个类。然后,Ajax.NET 将浏览指定的类,来查找标有 [b]AjaxMethodAttribute[/b] 的所有方法,其中 Sample 类有一个 [b]GetMessageOfTheDay[/b]。
完成后,剩下唯一要做的就是在 JavaScript 中使用它。Ajax.NET 自动创建与注册的类具有相同名称的 JavaScript 变量(在本例中将为 [b]Sample[/b]),它提供与 [b]AjaxMethod[/b] 具有相同名称的函数(在本例中为 [b]GetMessageOfTheDay[/b])。如下所示。
<script language="javascript">
Sample.GetMessageOfTheDay(GetMessageOfTheDay_CallBack);
function GetMessageOfTheDay_CallBack(response)
{
alert(response.value);
}
</script>
除了 JavaScript 回调函数以外,JavaScript [b]GetMessageOfTheDay[/b] 还需要与其服务器端对应部分相同的参数(在此情况下,没有参数),以便在完成时执行并传递响应。在此,我们看到 AJAX 在工作时的异步特性,因为对 [b]GetMessageOfTheDay[/b] 的调用不阻碍执行其他 JavaScript 代码,也不阻碍用户继续在页上进行操作。完成服务器端处理时,Ajax.NET 调用指定的回调函数 [b]GetMessageOfTheDay_CallBack[/b],并向其传递由服务器端返回值组成的响应。
服务器端代码和 JavaScript 代码之间的映射可能有些混乱。图 1 简要显示了服务器端代码和 JavaScript 代码,以及两者之间的映射。
//C# public static DataTable GetShippingCountries(); public static DataView GetCountryStates(int countryId); 'VB.NET Public Shared Function GetShippingCountries() As DataTable Public Shared Function GetCountryStates(ByVal countryId As Integer) As DataView现在,让我们转到相反面,创建简单的 Web 窗体。
<asp:DropDownList ID="countries" Runat="server" /> <asp:DropDownList ID="states" Runat="server" /> <asp:Button ID="submit" Runat="server" Text="Submit" />Page_Load 事件同样简单,和前述的 Web 窗体一样。我们使用数据访问层来检索可用的国家/地区,并将其绑定到 [b]countriesDropDownList[/b] 中。
//C#
if (!Page.IsPostBack)
{
countries.DataSource = DAL.GetShippingCountries();
countries.DataTextField = "Country";
countries.DataValueField = "Id";
countries.DataBind();
countries.Items.Insert(0, new ListItem("Please Select", "0"));
}
通常,代码到此为止。首先,我们将创建要从 JavaScript 调用的服务器端函数。
'VB.NET <Ajax.AjaxMethod()> _ Public Function GetStates (ByVal countryId As Integer) As DataView Return DAL.GetCountryStates(countryId) End Function
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有