更新:2007 年 11 月
以使用者定義的樣板顯示資料來源的值。ListView 控制項可讓使用者選取、排序、刪除、編輯和插入資料錄。
命名空間:
System.Web.UI.WebControls 組件:
System.Web.Extensions (在 System.Web.Extensions.dll 中)
<ToolboxBitmapAttribute(GetType(ListView), "ListView.ico")> _
<ControlValuePropertyAttribute("SelectedValue")> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class ListView _
Inherits DataBoundControl _
Implements INamingContainer, IPageableItemContainer
[ToolboxBitmapAttribute(typeof(ListView), "ListView.ico")]
[ControlValuePropertyAttribute("SelectedValue")]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ListView : DataBoundControl, INamingContainer,
IPageableItemContainer
[ToolboxBitmapAttribute(typeof(ListView), L"ListView.ico")]
[ControlValuePropertyAttribute(L"SelectedValue")]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class ListView : public DataBoundControl,
INamingContainer, IPageableItemContainer
/** @attribute ToolboxBitmapAttribute(ListView, "ListView.ico") */
/** @attribute ControlValuePropertyAttribute("SelectedValue") */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class ListView extends DataBoundControl implements INamingContainer,
IPageableItemContainer
public class ListView extends DataBoundControl implements INamingContainer, IPageableItemContainer
ListView 控制項是用於顯示資料來源中的值。這個控制項類似 GridView 控制項,不過是以使用者定義的樣板顯示資料,而不是使用資料列欄位。您可以建立自己的樣板,更靈活地控制資料的顯示方式。
ListView 控制項支援下列功能:
若要了解 ASP.NET 可用的其他資料繫結控制項,請參閱 ASP.NET 資料繫結 Web 伺服器控制項概觀。
樣板
繫結至資料來源
資料作業
當 ListView 控制項繫結至資料來源控制項時,ListView 控制項可利用資料來源控制項的功能,提供自動排序、插入、更新和刪除功能。
注意事項: |
|---|
對於其他類型的資料來源,ListView 控制項可以提供排序、插入、更新和刪除的支援。不過,為了實作這些作業,您必須在適當的事件處理常式中建立程式碼。 |
因為 ListView 控制項使用樣板,所以無法自動產生用來執行更新、刪除、插入、排序或選取作業的按鈕。您必須手動將這些按鈕加入適當的樣板中。ListView 控制項可辨認 CommandName 屬性設定為特定值的某些按鈕。下表列出 ListView 控制項可辨認的按鈕及其功能。
與 [刪除] 按鈕 (立即刪除目前資料項目) 不同,按一下 [編輯] 按鈕時,ListView 控制項會以編輯模式顯示目前項目。在編輯模式中,目前資料項目會顯示 EditItemTemplate 屬性包含的內容。一般來說,在編輯項目樣板中,[編輯] 按鈕會取代為 [更新] 和 [取消] 按鈕。適用於欄位資料型別的輸入控制項 (例如 TextBox 或 CheckBox 控制項),通常也會顯示欄位的值讓使用者修改。按一下 [更新] 按鈕可更新資料來源中的資料錄,而按一下 [取消] 按鈕則會取消編輯作業。
當 InsertItemPosition 屬性設定為 FirstItem 或 LastItem 時會啟用插入功能。這個屬性定義插入項目的 InsertItemTemplate 樣板會在何處呈現。插入項目樣板通常包含 [插入] 和 [取消] 按鈕,且會顯示空白輸入控制項,供使用者輸入新資料錄的值。按一下 [插入] 按鈕可在資料來源中插入資料錄,而按一下 [取消] 按鈕則會清除所有欄位。
排序
ListView 控制項可讓使用者按一下 [排序] 按鈕來排序項目。排序功能是在按鈕的 CommandArgument 屬性中定義,這個屬性包含要排序的資料行。
分頁
事件
下表列出 ListView 控制項支援的事件。
事件
|
說明
|
|---|
ItemCanceling |
當按一下 [取消] 按鈕 (CommandName 屬性設定為 "Cancel" 的按鈕) 時,但是在 ListView 控制項取消插入或編輯作業之前發生。這個事件通常用於停止取消作業。
|
ItemCommand |
按一下 ListView 控制項中的按鈕時會發生這個事件。在控制項中按一下按鈕時,這個事件通常用於執行自訂工作。
|
ItemCreated |
當 ListView 控制項中建立新的項目時發生。當建立項目時,這個事件通常用於修改項目的內容。
|
ItemDataBound |
當 ListView 控制項中的資料項目繫結至資料時發生。當項目繫結至資料時,這個事件通常用於修改項目的內容。
|
ItemDeleted |
當按一下 [刪除] 按鈕 (CommandName 屬性設定為 "Delete" 的按鈕) 或呼叫 DeleteItem 方法時,在 ListView 控制項從資料來源中刪除資料錄之後發生。這個事件通常用於檢查刪除作業的結果。
|
ItemDeleting |
當按一下 [刪除] 按鈕 (CommandName 屬性設定為 "Delete" 的按鈕) 或呼叫 DeleteItem 方法時,但是在 ListView 控制項從資料來源中刪除資料錄之前發生。這個事件通常用於確認或取消刪除作業。
|
ItemEditing |
當按一下 [編輯] 按鈕 (CommandName 屬性設定為 "Edit" 的按鈕) 時,但是在 ListView 控制項進入編輯模式之前發生。這個事件通常用於取消編輯作業。
|
ItemInserted |
當按一下 [插入] 按鈕 (CommandName 屬性設定為 "Insert" 的按鈕) 或呼叫 InsertNewItem 方法時,在 ListView 控制項從資料來源插入新的資料錄之後發生。通常使用這個事件,檢查更新作業的結果。
|
ItemInserting |
當按一下 [插入] 按鈕 (CommandName 屬性設定為 "Insert" 的按鈕) 或呼叫 InsertNewItem 方法時,在 ListView 控制項插入資料錄之前發生。這個事件通常用於取消插入作業,或用於變更或驗證新項目的值。
|
ItemUpdated |
當按一下 [更新] 按鈕 (CommandName 屬性設定為 "Update" 的按鈕) 或呼叫 UpdateItem 方法時,在 ListView 控制項更新資料錄之後發生。這個事件通常用於檢查更新作業的結果。
|
ItemUpdating |
當按一下 [更新] 按鈕 (CommandName 屬性設定為 "Update" 的按鈕) 或呼叫 UpdateItem 方法時,但是在 ListView 控制項更新資料錄之前發生。這個事件通常用於取消更新作業,或用於變更或驗證所編輯項目的值。
|
LayoutCreated |
在 ListView 控制項中建立 LayoutTemplate 樣板時發生。這個事件通常用於在建立樣板之後執行工作。
|
PagePropertiesChanged |
當頁面屬性變更時,在 ListView 控制項設定新的值之後發生。
|
PagePropertiesChanging |
當資料頁屬性變更時,但是在 ListView 控制項設定新的值之前發生。
|
SelectedIndexChanged |
當按一下 [選取] 按鈕 (CommandName 屬性設定為 "Select" 的按鈕) 時,在 ListView 控制項處理選取作業之後發生。在控制項中選取項目之後,這個事件通常用於執行自訂作業。
|
SelectedIndexChanging |
當按一下 [選取] 按鈕 (CommandName 屬性設定為 "Select" 的按鈕) 時,但是在 ListView 控制項處理選取作業之前發生。這個事件通常用於取消選取作業。
|
Sorted |
當按一下 [排序] 按鈕 (CommandName 屬性設定為 "Sort" 的按鈕) 或呼叫 Sort 方法時,在 ListView 控制項處理排序作業之後發生。在使用者按一下 [排序] 按鈕且排序資料之後,這個事件通常用於執行自訂工作。
|
Sorting |
當按一下 [排序] 按鈕 (CommandName 屬性設定為 "Sort" 的按鈕) 或呼叫 Sort 方法時,但是在 ListView 控制項處理排序作業之前發生。這個事件通常用於取消排序作業或執行自訂排序常式。
|
下列範例顯示如何使用 ListView 控制項,利用 HTML 表格顯示資料庫中的資料錄。值是透過 LinqDataSource 控制項擷取。
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" ID="tbl1" runat="server">
<tr runat="server" style="background-color: #98FB98">
<th runat="server">ID</th>
<th runat="server">Account Number</th>
<th runat="server">Name</th>
<th runat="server">Preferred Vendor</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
</td>
<td>
<asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /></td>
<td>
<asp:CheckBox ID="PreferredCheckBox" runat="server"
Checked='<%# Eval("PreferredVendorStatus") %>' Enabled="False" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Add a LINQ -->
<!-- to SQL class to the project to map to a table in -->
<!-- the database. -->
<asp:LinqDataSource ID="VendorsDataSource" runat="server"
ContextTypeName="AdventureWorksClassesDataContext"
Select="new (VendorID, AccountNumber, Name, PreferredVendorStatus)"
TableName="Vendors" Where="ActiveFlag == @ActiveFlag">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="ActiveFlag" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" ID="tbl1" runat="server">
<tr runat="server" style="background-color: #98FB98">
<th runat="server">ID</th>
<th runat="server">Account Number</th>
<th runat="server">Name</th>
<th runat="server">Preferred Vendor</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
</td>
<td>
<asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /></td>
<td>
<asp:CheckBox ID="PreferredCheckBox" runat="server"
Checked='<%# Eval("PreferredVendorStatus") %>' Enabled="False" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Add a LINQ -->
<!-- to SQL class to the project to map to a table in -->
<!-- the database. -->
<asp:LinqDataSource ID="VendorsDataSource" runat="server"
ContextTypeName="AdventureWorksClassesDataContext"
Select="new (VendorID, AccountNumber, Name, PreferredVendorStatus)"
TableName="Vendors" Where="ActiveFlag == @ActiveFlag">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="ActiveFlag" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>
</form>
</body>
</html>
下列範例顯示如何使用 ListView 控制項,利用 div 項目在流程配置中顯示值。值是透過 SqlDataSource 控制項擷取。
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Flow Layout Example</title>
<style type="text/css">
.plainBox {
font-family: Verdana, Arial, sans-serif;
font-size: 11px;
background: #ffffff;
border:1px solid #336666;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Flow Layout Example</h3>
Select the color:
<asp:DropDownList ID="ColorList" runat="server"
AutoPostBack="True"
DataSourceID="ColorDataSource"
DataTextField="Color"
DataValueField="Color">
</asp:DropDownList><br /><br />
<asp:ListView runat="server" ID="ProductListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID">
<LayoutTemplate>
<div runat="server" id="lstProducts">
<div runat="server" id="itemPlaceholder" />
</div>
<asp:DataPager ID="DataPager1" runat="server" PageSize="5" >
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Image ID="ProductImage" runat="server"
ImageUrl='<%# "~/images/thumbnails/" & Eval("ThumbnailPhotoFileName") %>' />
<div class="plainBox" runat="server">
<asp:HyperLink ID="ProductLink" runat="server" Text='<%# Eval("Name") %>'
NavigateUrl='<%# "ProductDetails.aspx?productID=" & Eval("ProductID") %>' />
<br /><br />
<b>Price:</b>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("ListPrice", "{0:c}")%>' /> <br />
</div>
<br />
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice,
PF.ThumbnailPhotoFileName, P.Size
FROM Production.Product AS P
INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID
INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID
WHERE P.Color = @Color" >
<SelectParameters>
<asp:ControlParameter ControlID="ColorList" Name="Color"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DISTINCT Color FROM Production.Product">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Flow Layout Example</title>
<style type="text/css">
.plainBox {
font-family: Verdana, Arial, sans-serif;
font-size: 11px;
background: #ffffff;
border:1px solid #336666;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Flow Layout Example</h3>
Select the color:
<asp:DropDownList ID="ColorList" runat="server"
AutoPostBack="True"
DataSourceID="ColorDataSource"
DataTextField="Color"
DataValueField="Color">
</asp:DropDownList><br /><br />
<asp:ListView runat="server" ID="ProductListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID">
<LayoutTemplate>
<div runat="server" id="lstProducts">
<div runat="server" id="itemPlaceholder" />
</div>
<asp:DataPager runat="server" PageSize="5" >
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Image ID="ProductImage" runat="server"
ImageUrl='<%# "~/images/thumbnails/" + Eval("ThumbnailPhotoFileName") %>' />
<div class="plainBox" runat="server">
<asp:HyperLink ID="ProductLink" runat="server" Text='<%# Eval("Name") %>'
NavigateUrl='<%# "ProductDetails.aspx?productID=" + Eval("ProductID") %>' />
<br /><br />
<b>Price:</b>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("ListPrice", "{0:c}")%>' /> <br />
</div>
<br />
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice,
PF.ThumbnailPhotoFileName, P.Size
FROM Production.Product AS P
INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID
INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID
WHERE P.Color = @Color" >
<SelectParameters>
<asp:ControlParameter ControlID="ColorList" Name="Color"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DISTINCT Color FROM Production.Product">
</asp:SqlDataSource>
</form>
</body>
</html>
下列範例顯示如何使用 ListView 控制項來插入、刪除和更新資料錄。
安全性注意事項: |
|---|
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。如需詳細資訊,請參閱 指令碼攻擊概觀。 |
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
<style type="text/css">
.EditItem { background-color:#8FBC8F;}
.SelectedItem { background-color:#9ACD32; }
.InsertItem { background-color:#FFFACD;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
ConvertEmptyStringToNull="true"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblDepartments" width="640px" cellspacing="0">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="SelectButton" runat="server" Text="Select" CommandName="Select" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr class="SelectedItem" runat="server">
<td>
<asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<b>ID</b><br />
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Width="200px"
Text='<%#Bind("GroupName") %>' />
<br />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr class="InsertItem">
<td colspan="2">
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%#Bind("GroupName") %>' />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
UpdateCommand="UPDATE HumanResources.Department
SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID)"
DeleteCommand="DELETE FROM HumanResources.Department
WHERE (DepartmentID = @DepartmentID)"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
<style type="text/css">
.EditItem { background-color:#8FBC8F;}
.SelectedItem { background-color:#9ACD32; }
.InsertItem { background-color:#FFFACD;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
ConvertEmptyStringToNull="true"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblDepartments" width="640px" cellspacing="0">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="SelectButton" runat="server" Text="Select" CommandName="Select" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr class="SelectedItem" runat="server">
<td>
<asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<b>ID</b><br />
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Width="200px"
Text='<%#Bind("GroupName") %>' />
<br />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr class="InsertItem">
<td colspan="2">
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%#Bind("GroupName") %>' />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
UpdateCommand="UPDATE HumanResources.Department
SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID)"
DeleteCommand="DELETE FROM HumanResources.Department
WHERE (DepartmentID = @DepartmentID)"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</asp:SqlDataSource>
</form>
</body>
</html>
System..::.Object
System.Web.UI..::.Control
System.Web.UI.WebControls..::.WebControl
System.Web.UI.WebControls..::.BaseDataBoundControl
System.Web.UI.WebControls..::.DataBoundControl
System.Web.UI.WebControls..::.ListView
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
Windows Vista, Windows XP SP2, Windows Server 2003
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5
參考
其他資源