日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

利用數(shù)據(jù)綁定和模板創(chuàng)建Atlas應(yīng)用程序_Web服務(wù)器教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

一、 簡介

  本文將向你展示如何使用微軟新的Web開發(fā)技術(shù)(代碼名為Atlas)來實現(xiàn)數(shù)據(jù)綁定和模板。如果你已經(jīng)理解什么是Atlas,其主要設(shè)計目的及其主要組件,那么你在閱讀本文時將最大程度地受益。

  本文將向你展示:

  · 把一個客戶端listView控件綁定到一個dataSource控件。

  · 使用模板顯示數(shù)據(jù)。

  前提

  為了完成本文中的示例程序,你需要具備下列條件:

  · Microsoft Visual Studio 2005和.NET Framework 2.0。有關(guān)下載信息,請訪問.NET Framework Developer Center Web站點。

  · 要把Atlas包安裝到你的計算機上。這個MSI安裝器文件包括一個Visual Studio Content Installer(.vsi)以便在Visual Studio中創(chuàng)建一個空白的Atlas Web應(yīng)用程序。在本文中,我們省略了如何安裝ASP.NET Atlas內(nèi)容。

  二、 創(chuàng)建Atlas應(yīng)用程序

  首先,你要在Visual Studio中創(chuàng)建一個Atlas Web應(yīng)用程序。當你使用Visual Studio工程模板來創(chuàng)建一個新的空白Atlas Web應(yīng)用程序時,Visual Studio會創(chuàng)建一個正常的具有下列一些其它項的Web站點文件夾結(jié)構(gòu):

  · 一個名為Microsoft.Web.Atlas.dll的可執(zhí)行文件,它駐留在Bin文件夾下以提供服務(wù)器端功能。

  · 一個文件Web.config,用于設(shè)置Atlas應(yīng)用程序。

  在Visual Studio中創(chuàng)建一個新的Atlas Web應(yīng)用程序

  1. 在"File"菜單下,點擊"New",然后點擊"Web Site"。

  2. 在"New Web Site"對話框中,選擇"ASP.NET Atlas Web Site"模板項。

  3. 在"Location"列表中,選擇"File System"。

  4. 指定程序的一個路徑和開發(fā)語言,然后點擊"OK"。

  

三、 提供應(yīng)用程序測試數(shù)據(jù)

  在這一部分中,你要創(chuàng)建數(shù)據(jù)綁定程序所要使用的兩項內(nèi)容:

  · 一個數(shù)據(jù)源對象-它通過提供一些測試數(shù)據(jù)和類SQL語句來模擬一個數(shù)據(jù)庫。

  · 一個Web服務(wù)-它連接到數(shù)據(jù)源對象并且把該數(shù)據(jù)提供給一個使用Atlas組件創(chuàng)建的UI。

  首先,你要創(chuàng)建數(shù)據(jù)源對象。

  創(chuàng)建數(shù)據(jù)源對象

  1. 在解決方案資源管理器中,右擊站點名字,然后點擊"Add New Item"。

  2. 在"Add New Item"對話框中,選擇"Class",并且命名這個類為SampleRow(沒有文件擴展名)。

  3. 為該類選擇開發(fā)語言,然后點擊"Add"按鈕。

  4. 當系統(tǒng)提問你,是否你想把這個類文件放到App_Code文件夾下時,點擊"Yes"。

  5. 在編輯器中,從已有類中刪除任何現(xiàn)有代碼。

  6. 把下列代碼粘貼到這個類中以創(chuàng)建一個數(shù)據(jù)源對象。

using System;
using System.Collections;
using System.ComponentModel;
public class SampleRow{
private string _name;
private string _description;
private int _id;
[DataObjectField(true, true)]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataObjectField(false)]
[DefaultValue("New row")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataObjectField(false)]
[DefaultValue("")]
public string Description
{
get { return _description; }
set { _description = value; }
}
public SampleRow()
{
_id = -1;
}
public SampleRow(int id, string name, string description)
{
_id = id;
_name = name;
_description = description;
}
}


 

 7. 保存并關(guān)閉文件。

  下一步是創(chuàng)建一個Web服務(wù),由該服務(wù)為ASP.NET Web頁面提供來自于數(shù)據(jù)源對象的數(shù)據(jù)。

  創(chuàng)建Web服務(wù)為頁面提供數(shù)據(jù)

  1. 在解決方案資源管理器中,右擊站點名字,然后點擊"Add New Item"。

  2. 在"Add New Item"對話框中,在Visual Studio已安裝的模板下,選擇"Web Service"。

  3. 指定文件名為DataService.asmx并且不點選"Place code in separate file"復(fù)選框。

  4. 選擇你想使用的語言。

  5. 點擊"Add"。

  6. 在編輯器中,從現(xiàn)有類中刪除任何現(xiàn)有代碼。

  7. 把下列代碼粘貼到這個類中以創(chuàng)建一個數(shù)據(jù)源對象。

<%@ WebService Language="C#" Class="SampleDataService" %>
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SampleDataService : DataService {
static List<SampleRow> _data;
static int _nextId;
static object _dataLock = new object();
private static List<SampleRow> Data {
 get {
  if (_data == null) {
   lock (_dataLock) {
    if (_data == null) {
     _data = new List<SampleRow>();
      _data.Add(new SampleRow(0, "A. Datum Corporation", "http://www.adatum.com"));
      _data.Add(new SampleRow(1, "Adventure Works", "http://www.adventure-works.com"));
      _data.Add(new SampleRow(2, "Alpine Ski House", "http://www.alpineskihouse.com"));
      _data.Add(new SampleRow(3, "Baldwin Museum of Science?", "http://www.baldwinmuseumofscience.com"));
      _data.Add(new SampleRow(4, "Blue Yonder Airlines","http://www.blueyonderairlines.com"));
      _data.Add(new SampleRow(5, "City Power & Light","http://www.cpandl.com"));
      _data.Add(new SampleRow(6, "Coho Vineyard","http://www.cohovineyard.com"));
      _data.Add(new SampleRow(7, "Contoso, Ltd","http://www.contoso.com"));
      _data.Add(new SampleRow(8, "Graphic Design Institute",
"http://www.graphicdesigninstitute.com"));
      _nextId = 9;
     }
    }
   }
   return _data;
  }
 }
[DataObjectMethod(DataObjectMethodType.Delete)]
public void DeleteRow(int id) {
 foreach (SampleRow row in _data) {
  if (row.Id == id) {
   lock (_dataLock) {
    _data.Remove(row);
   }
   break;
  }
 }
}
[DataObjectMethod(DataObjectMethodType.Select)]
public SampleRow[] SelectRows() {
 return SampleDataService.Data.ToArray();
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public SampleRow InsertRow(string organization, string url) {
 SampleRow newRow;
 lock (_dataLock) {
  newRow = new SampleRow(_nextId++, organization, url);
  _data.Add(newRow);
 }
 return newRow;
}
[DataObjectMethod(DataObjectMethodType.Update)]
public void UpdateRow(SampleRow updateRow) {
 foreach (SampleRow row in _data) {
  if (row.Id == updateRow.Id) {
   row.Name =updateRow.Name;
   row.Description = updateRow.Description;
   break;
  }
 }
}
}


  8. 保存并關(guān)閉該文件。

 

四、 創(chuàng)建宿主控件的Web頁面

  在這一部分中,你將創(chuàng)建一個新的ASP.NET Web頁面來宿主數(shù)據(jù)綁定控件和模板。

  創(chuàng)建一個Web頁面

  1. 添加一新的ASP.NET頁面到你的工程并且命名它為DataBinding.aspx。

  注意 確保你清除了"Place code in separate file"復(fù)選框。在此,你必須創(chuàng)建單個ASP.NET Web頁面。

  2. 切換到"Source view"。

  3. 在@Page指令中,把Title屬性設(shè)置為"Atlas Data-Binding Walkthrough",如下面的示例所示:

<%@ Page Language="C#" Title="Atlas Data-binding Walkthrough" %>

  4. 把下列標注內(nèi)容復(fù)制并粘貼到在@Page指令下的文件中:

<!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>
</head>
<body>
�。糵orm id="main" runat="server">
  <atlas:ScriptManager runat="server" ID="scriptManager" />
 �。糷3>Data-Bound ListView</h3>
 �。糳iv id="dataContents"></div>
 �。糳iv style="visibility:hidden;display:none">
  �。糳iv id="masterTemplate">
   �。糳iv id="masterItemTemplate">
    �。糱><span id="masterName"></span></b>
    �。糱r />
     <asp:linkbutton id="LinkButton1" runat="server">
     �。約pan id="masterDescription"></span>
    �。�/asp:linkbutton><br />
    </div><br/>
  �。�/div>
  �。糳iv id="masterNoDataTemplate">No data</div>
  </div>
�。�/form>
<script type="text/xml-script">
�。紁age xmlns:script="http://schemas.microsoft.com/xml-script/2005">
�。糲omponents>
�。糳ataSource id="dataSource" serviceURL="DataService.asmx" autoLoad="true" />
 <listView id="dataContents" itemTemplateParentElementId="masterTemplate"
propertyChanged="onChange">
�。糱indings>
 �。糱inding dataContext="dataSource" dataPath="data" property="data"/>
�。�/bindings>
 <layoutTemplate>
 �。紅emplate layoutElement="masterTemplate"/>
�。�/layoutTemplate>
 <itemTemplate>
 �。紅emplate layoutElement="masterItemTemplate">
 �。糽abel id="masterName">
  �。糱indings>
   �。糱inding dataPath="Name" property="text"/>
  �。�/bindings>
 �。�/label>
  <hyperLink id="masterDescription">
  �。糱indings>
   �。糱inding dataPath="Description" property="text"/>
  �。�/bindings>
 �。�/hyperLink>
�。�/template>
�。�/itemTemplate>
 <emptyTemplate>
 �。紅emplate layoutElement="masterNoDataTemplate"/>
 </emptyTemplate>
</listView>
</components>
</page>
</script>
</body>
</html>


  

注意,在<script>元素內(nèi),存在一些聲明性元素-它們指定Atlas客戶端控件和數(shù)據(jù)綁定布局。該數(shù)據(jù)是由服務(wù)器端服務(wù)所指定的,而UI是由綁定到它們的客戶端控件所提供的。注意,你可以使用這種聲明性語法來指定當應(yīng)用程序事件發(fā)生時會發(fā)生什么,正如你用JavaScript代碼所能夠?qū)崿F(xiàn)的功能一樣。請檢查上面標注中的<dataSource>元素。它有一個屬性serviceURL來指向檢索數(shù)據(jù)的Web服務(wù),還有一個autoLoad來指示當對象被創(chuàng)建時應(yīng)該立即檢索該數(shù)據(jù)。結(jié)果是,當應(yīng)用程序加載時,數(shù)據(jù)就會立即從數(shù)據(jù)源中進行檢索并通過頁面中的模板進行顯示。

  5. 保存并關(guān)閉該頁面。

  測試頁面

  1. 運行DataBinding.aspx頁面。

  2. 確保在頁面裝載以后,有一組公司及其各自的URL顯示出來。

  五、 總結(jié)

  在本文中,你學(xué)習(xí)了怎樣"Atlas化"客戶端控件以存取服務(wù)器端數(shù)據(jù)服務(wù)。這里所使用的數(shù)據(jù)綁定語法非常類似于用于把ASP.NET服務(wù)器控件綁定到數(shù)據(jù)的指令語法。具體地說,你學(xué)習(xí)了如何把一個客戶端listView控件綁定到一個DataSource控件,以及如何使用一個聲明性layoutTemplate元素和其它Atlas控件和標準HTML標注來指定數(shù)據(jù)在頁面上的生成方式。

來源:網(wǎng)絡(luò)搜集//所屬分類:Web服務(wù)器教程/更新時間:2013-04-14
相關(guān)Web服務(wù)器教程