ASP.NET AJAX 4.0 模版的使用 
1. Introduction: 
當Microsoft發布了它的ASP.NET 3.0時也同時發布了它的AJAX平臺也就是ASP.NET AJAX。 不過說實話,當時相比其它的AJAX平臺,它沒有什么很突出的功能。不過當我評估ASP.NET AJAX 4.0的時候,我確實被它的特征給震住了。新的特征完全專注于瀏覽器技術,比如XHTML和javascript。 我非常欽佩ASP.NET AJAX小組。試試看看AJAX4.0的新特征: 
Template based client side programming 
DataView and DataContext 
Live Data Binding 
2. Template Programming 
模版形式提供了一個可以設計Web UI樣式的模式并且能夠給運行時的數據添加位置標記。下面這個例子中,我設計了一個web頁面來顯示AdventureWorks數據庫的產品數據通過ADO.NET data service. 整個模式如下: 
復制代碼 代碼如下:
public class AWProductDataService : DataService 
{ 
public static void InitializeService(IDataServiceConfiguration config) 
{ 
config.SetEntitySetAccessRule("*", EntitySetRights.All); 
} 
}
復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="ClientTemplateAndDataViewDemo.aspx.cs" 
Inherits="CoreEnhancements.AJAX.ClientTemplateAndDataViewDemo" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
<title>Microsoft Tech.Ed - Client-side Templating Demo</title> 
<style type="text/css"><!-- 
.sys-template {display:none} 
--></style><style type="text/css" bogus="1"> .sys-template {display:none} 
</style> 
<script type="text/javascript" src="../scripts/MicrosoftAjax.debug.js" src="scripts/MicrosoftAjax.debug.js"></script> 
<script type="text/javascript" src="../scripts/MicrosoftAjaxTemplates.debug.js" src="scripts/MicrosoftAjaxTemplates.debug.js"><!-- 
// --></script> 
<script type="text/javascript" src="../scripts/MicrosoftAjaxAdoNet.debug.js" src="scripts/MicrosoftAjaxAdoNet.debug.js"><!-- 
// --></script> 
<script type="text/javascript"><!-- 
var dataContext = new Sys.Data.AdoNetDataContext(); 
dataContext.set_serviceUri("AWProductDataService.svc"); 
dataContext.initialize(); 
// --></script> 
</head> 
<body xmlns:sys="javascript:Sys" xmlns:dataview="javascript:Sys.UI.DataView" 
sys:activate="*"> 
<form runat="server"> 
<div> 
<table> 
<thead> 
<tr> 
<td>Name</td> 
<td>List Price</td> 
<td>Size</td> 
<td>Weight</td> 
</tr> 
</thead> 
<tbody sys:attach="dataview" dataview:autofetch="true" 
dataview:dataprovider="{{ dataContext }}" 
dataview:fetchoperation="Products"> 
<tr> 
<td>{binding Name }</td> 
<td>{binding ListPrice}</td> 
<td>{binding Size}</td> 
<td>{binding Weight}</td> 
</tr> 
</tbody> 
</table> 
</div> 
</form> 
</body> 
</html>
復制代碼 代碼如下:
var dataContext = new Sys.Data.AdoNetDataContext(); dataContext.set_serviceUri("AWProductDataService.svc"); dataContext.initialize(); 
4. Data View
這里有個基本組件用來為模版展示數據,它定義在System.UI.DataView中。它十分類似于服務器端的支持綁定任何Javascript對象或數據或是ASP.NET AJAX組件的數據源組件。它有連個屬性來進行數據綁定:
data - To bind a JavaScript array or object 
dataprovider - To bind to a WCF service 
如果你需要運行這個程序,你需要添加下面幾個客戶端的AJAX庫。
MicrosoftAjax.js 
MicrosoftAjaxTemplates 
MicrosoftAjaxAdoNet 
下面這個圖展示了總體的一個使用模版編程的概念模型:
數據最后的展示如下:
| 
 
 | 
新聞熱點
疑難解答
圖片精選