本文英文原版及代碼下載:
http://aspnet.4guysfromrolla.com/articles/122607-1.aspx
ASP.NET 3.5's ListView and DataPager—Part1 用ListView控件展示數據
導言:
在2007年11月微軟發佈了ASP.NET 3.5,正如文章《An Overview of ASP.NET 3.5 and Visual Studio 2008》(http://aspnet.4guysfromrolla.com/articles/112107-1.aspx)提到的那樣,ASP.NET 3.5包含了2個新的ASP.NET data controlsb控件:ListView 和 DataPager.簡單的說,ListView控件在實現分頁、排序、編輯、插入等功能之外,還提供了一些非常靈活的方法來展示數據。而DataPager控件可以與一個ListView控件一道實現一個分頁接口(paging interface).
在ASP.NET 3.5之前,開發人員要展示一個記錄集(set of records)時,要使用GridView, DataList和 Repeater控件,GridView雖然有豐富的數據特性(data features),開外形看起來方方正正的;DataList 和 Repeater控件雖然外形靈活,但數據特性又不如GridView、DetailsView以及FormView 豐富.而ListView控件恰好兼顧外觀的靈活性和內置數據特性.
本文作爲ListView and DataPager控件系列的第一篇, 考察ListView裏可用到的多個模板,並演示如何呈現數據.
ListView概述
很多ASP.NET data Web控件自動的用一些額外的標記將要綁定的數據框起來.比如,GridView控件將它要呈現的數據置於一個HTML <table>裏, 將綁定每條記錄放在一個table row (<tr>)裏,每列作爲一個單元又放在row (<tr>)裏.因此,GridView的外觀看起來非常的方正.雖然頁面開發員可以用TemplateFields以及其它的工具來定製GridView的外觀,但GridView的輸出結果(output)依然包含在一個<table>標籤裏.而ListView控件則不然,不會用額外的標記將要綁定的數據框起來,而由我們(頁面開發人員)負責來對控件的HTML呈現效果進行控制.以下是
listView控件的的11個模板:
.AlternatingItemTemplate
.EditItemTemplate
.EmptyDataTemplate
.EmptyItemTemplate
.GroupSeparatorTemplate
.GroupTemplate
.InsertItemTemplate
.ItemSeparatorTemplate
.ItemTemplate
.LayoutTemplate
.SelectedItemTemplate
其中比較重要的模式是LayoutTemplate 和ItemTemplate。其中LayoutTemplate指定了ListView的外觀標記(encasing markup),而ItemTemplate指定的標記用於生成綁定到ListView的每條記錄。比如,我們要展示一個清單,我們可以這樣做:
<asp:ListView ID="..." runat="server" DataSourceID="..."> DataSourceID="...">width="100%" bgColor=#cccccc><asp:ListView
ID="..." runat="server" DataSourceID="...">
<LayoutTemplate>
<ol>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ol>
</LayoutTemplate>
<ItemTemplate>
<li><%# Eval("columnName") %></li>
</ItemTemplate>
</asp:ListView>
當分別指定ListView的LayoutTemplate和ItemTemplate之後,我們需要告知LayoutTemplate:“聽好,對要展示的每條記錄,將呈現條目標記(rendered item markup)放在這裏”。具體方法是添加一個服務器端的控件,其ID由ListView控件的ItemPlaceholderID屬性指定. 該屬性默認值爲"itemPlaceholder", 如上述代碼所示.當然我也可以使用其它的名稱,只需要爲ListView的ItemPlaceholdID屬性指定一個值即可.
要在ItemTemplate裏輸出某個字段值,使用綁定語法<%# Eval("columnName")%>.
假定上面的listView綁定到employees數據庫表,我們想在ItemTemplate裏的<li>元素輸出FullName列,那麼怎樣在ListView裏聲明呈現聲明(rendered markup)呢?
我們先處理LayoutTemplate,如下:
<ol>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ol>
接下來,將綁定到ListView控件的每一條記錄呈現在ItemTemplate模板裏,比如這樣:
<li>Scott Mitchell</li>
<li>Sam Smith</li>
<li>Jisun Lee</li>
<li>Andrew Fuller</li>
<li>Edgar Johnson</li>
<li>Ellen Plank</li>
ItemTemplate的rendered markup放置在PlaceHolder控件裏(因爲其ID與ListView控件的 ItemPlaceholderID值吻合),最終結果如下:
<ol>
<li>Scott Mitchell</li>
<li>Sam Smith</li>
<li>Jisun Lee</li>
<li>Andrew Fuller</li>
<li>Edgar Johnson</li>
<li>Ellen Plank</li>
</ol>
An Example of Displaying Simple Data with the ListView
在本文結尾部分可下載到示例代碼,爲一個ASP.NET 3.5的website,代碼演示瞭如何使用ListView控件.代碼使用的是微軟Access類型的Northwind數據庫,其放置在App_Data文件夾下.而"Simple Data"代碼演示瞭如何使用ListView展示Northwind數據庫裏Products表的記錄.我們用一個AccessDataSource控件來檢索Products數據表,並將最終結果綁定到ListView.
具體來說,listView控件最開始將標題"Product lising"顯示在一個<h3>元素。然後將products信息放置在一個<blockquote>元素裏,該元素具有對頁面輸出(output)進行縮進的效果。product的name, category, unit price和quantity都展示在一個單元格里.每條產品由水平規則元素(<hr>)分割開來,當然這是在ItemSeparatorTemplate裏定義的.
ListView 和accessDataSource的聲明代碼如下:
<asp:ListView ID="ProductList" runat="server" DataSourceID="ProductDataSource">
<LayoutTemplate>
<h3>Product Listing</h3>
<blockquote>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</blockquote>
</LayoutTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
<ItemTemplate>
<h4><%#Eval("ProductName")%> (<%# Eval("CategoryName") %>)</h4>
Available at <%#Eval("UnitPrice", "{0:c}")%>,
with <%#Eval("QuantityPerUnit")%>.
</ItemTemplate>
</asp:ListView>
<asp:AccessDataSource ID="ProductDataSource" runat="server"
DataFile="~/App_Data/Northwind.mdb"
SelectCommand="SELECT [ProductName], [QuantityPerUnit], [UnitPrice], [CategoryName] FROM [Alphabetical List of Products]">
</asp:AccessDataSource>
當訪問頁面時,listView控件將顯示爲如下的HTML:
<h3>Product Listing</h3>
<blockquote>
<h4>Chai (Beverages)</h4>
Available at $18.00,
with 10 boxes x 20 bags.
<hr />
<h4>Chang (Beverages)</h4>
Available at $19.00,
with 24 - 12 oz bottles.
<hr />
<h4>Aniseed Syrup (Condiments)</h4>
Available at $10.00,
with 12 - 550 ml bottles.
<hr />
<h4>Chef Anton's Cajun Seasoning (Condiments)</h4>
Available at $22.00,
with 48 - 6 oz jars.
<hr />
<h4>Grandma's Boysenberry Spread (Condiments)</h4>
Available at $25.00,
with 12 - 8 oz jars.
... Many products have been removed for brevity ...
</blockquote>
在客戶端瀏覽器裏,控件看起來像這樣:
圖
結語
ListView控件是ASP.NET 3.5裏新添加的,它像GridView一樣具有豐富的數據特性,同時在界面輸出方面具有更好的可塑性.正如本文探討的那樣,ListView的界面輸出由聲明代碼(markup),數據綁定表達式以及LayoutTemplate以及ItemTemplate裏添加的Web controls進行控制.此外,還有其它的一些模板,在後面涉及排序、分頁、編輯、插入功能的文章裏我們再進行探究.
祝編程快樂!