国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

利用數據綁定和模板創建Atlas應用程序

2024-07-21 02:29:15
字體:
來源:轉載
供稿:網友

  一、 簡介

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

  本文將向你展示:

  · 把一個客戶端listview控件綁定到一個datasource控件。

  · 使用模板顯示數據。

  前提

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

  · microsoft visual studio 2005和.net framework 2.0。有關下載信息,請訪問.net framework developer center web站點。

  · 要把atlas包安裝到你的計算機上。這個msi安裝器文件包括一個visual studio content installer(.vsi)以便在visual studio中創建一個空白的atlas web應用程序。在本文中,我們省略了如何安裝asp.net atlas內容。

  二、 創建atlas應用程序

  首先,你要在visual studio中創建一個atlas web應用程序。當你使用visual studio工程模板來創建一個新的空白atlas web應用程序時,visual studio會創建一個正常的具有下列一些其它項的web站點文件夾結構:

  · 一個名為microsoft.web.atlas.dll的可執行文件,它駐留在bin文件夾下以提供服務器端功能。

  · 一個文件web.config,用于設置atlas應用程序。

  在visual studio中創建一個新的atlas web應用程序

  1. 在"file"菜單下,點擊"new",然后點擊"web site"。

  2. 在"new web site"對話框中,選擇"asp.net atlas web site"模板項。

  3. 在"location"列表中,選擇"file system"。

  4. 指定程序的一個路徑和開發語言,然后點擊"ok"。

  三、 提供應用程序測試數據

  在這一部分中,你要創建數據綁定程序所要使用的兩項內容:

  · 一個數據源對象-它通過提供一些測試數據和類sql語句來模擬一個數據庫。

  · 一個web服務-它連接到數據源對象并且把該數據提供給一個使用atlas組件創建的ui。

  首先,你要創建數據源對象。

  創建數據源對象

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

  2. 在"add new item"對話框中,選擇"class",并且命名這個類為samplerow(沒有文件擴展名)。

  3. 為該類選擇開發語言,然后點擊"add"按鈕。

  4. 當系統提問你,是否你想把這個類文件放到app_code文件夾下時,點擊"yes"。

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

  6. 把下列代碼粘貼到這個類中以創建一個數據源對象。

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. 保存并關閉文件。

  下一步是創建一個web服務,由該服務為asp.net web頁面提供來自于數據源對象的數據。

  創建web服務為頁面提供數據

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

  2. 在"add new item"對話框中,在visual studio已安裝的模板下,選擇"web service"。

  3. 指定文件名為dataservice.asmx并且不點選"place code in separate file"復選框。

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

  5. 點擊"add"。

  6. 在編輯器中,從現有類中刪除任何現有代碼。

  7. 把下列代碼粘貼到這個類中以創建一個數據源對象。

<%@ 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. 保存并關閉該文件。

  四、 創建宿主控件的web頁面

  在這一部分中,你將創建一個新的asp.net web頁面來宿主數據綁定控件和模板。

  創建一個web頁面

  1. 添加一新的asp.net頁面到你的工程并且命名它為databinding.aspx。

  注意 確保你清除了"place code in separate file"復選框。在此,你必須創建單個asp.net web頁面。

  2. 切換到"source view"。

  3. 在@page指令中,把title屬性設置為"atlas data-binding walkthrough",如下面的示例所示:

<%@ page language="c#" title="atlas data-binding walkthrough" %>

  4. 把下列標注內容復制并粘貼到在@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>
 <form id="main" runat="server">
  <atlas:scriptmanager runat="server" id="scriptmanager" />
  <h3>data-bound listview</h3>
  <div id="datacontents"></div>
  <div >
   <div id="mastertemplate">
    <div id="masteritemtemplate">
     <b><span id="mastername"></span></b>
     <br />
     <asp:linkbutton id="linkbutton1" runat="server">
      <span id="masterdescription"></span>
     </asp:linkbutton><br />
    </div><br/>
   </div>
   <div id="masternodatatemplate">no data</div>
  </div>
 </form>
<script type="text/xml-script">
 <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
 <components>
 <datasource id="datasource" serviceurl="dataservice.asmx" autoload="true" />
 <listview id="datacontents" itemtemplateparentelementid="mastertemplate"
propertychanged="onchange">
 <bindings>
  <binding datacontext="datasource" datapath="data" property="data"/>
 </bindings>
 <layouttemplate>
  <template layoutelement="mastertemplate"/>
 </layouttemplate>
 <itemtemplate>
  <template layoutelement="masteritemtemplate">
  <label id="mastername">
   <bindings>
    <binding datapath="name" property="text"/>
   </bindings>
  </label>
  <hyperlink id="masterdescription">
   <bindings>
    <binding datapath="description" property="text"/>
   </bindings>
  </hyperlink>
 </template>
 </itemtemplate>
 <emptytemplate>
  <template layoutelement="masternodatatemplate"/>
 </emptytemplate>
</listview>
</components>
</page>
</script>
</body>
</html>

  注意,在<script>元素內,存在一些聲明性元素-它們指定atlas客戶端控件和數據綁定布局。該數據是由服務器端服務所指定的,而ui是由綁定到它們的客戶端控件所提供的。注意,你可以使用這種聲明性語法來指定當應用程序事件發生時會發生什么,正如你用javascript代碼所能夠實現的功能一樣。請檢查上面標注中的<datasource>元素。它有一個屬性serviceurl來指向檢索數據的web服務,還有一個autoload來指示當對象被創建時應該立即檢索該數據。結果是,當應用程序加載時,數據就會立即從數據源中進行檢索并通過頁面中的模板進行顯示。

  5. 保存并關閉該頁面。

  測試頁面

  1. 運行databinding.aspx頁面。

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

  五、 總結

  在本文中,你學習了怎樣"atlas化"客戶端控件以存取服務器端數據服務。這里所使用的數據綁定語法非常類似于用于把asp.net服務器控件綁定到數據的指令語法。具體地說,你學習了如何把一個客戶端listview控件綁定到一個datasource控件,以及如何使用一個聲明性layouttemplate元素和其它atlas控件和標準html標注來指定數據在頁面上的生成方式。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 凤庆县| 峨边| 甘洛县| 万全县| 达拉特旗| 长丰县| 金昌市| 瓦房店市| 甘洛县| 筠连县| 日喀则市| 桐城市| 深州市| 潢川县| 沙洋县| 皮山县| 滨海县| 喀喇| 盐城市| 文山县| 黔西| 永善县| 阿鲁科尔沁旗| 花莲县| 台州市| 安顺市| 安岳县| 电白县| 汉阴县| 华阴市| 西林县| 武城县| 永修县| 达拉特旗| 渝北区| 云龙县| 镇沅| 武安市| 宁波市| 泰安市|