ListView.ItemDataBound 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當資料項目繫結至 ListView 控制項中的資料時發生。
public:
event EventHandler<System::Web::UI::WebControls::ListViewItemEventArgs ^> ^ ItemDataBound;
public event EventHandler<System.Web.UI.WebControls.ListViewItemEventArgs> ItemDataBound;
member this.ItemDataBound : EventHandler<System.Web.UI.WebControls.ListViewItemEventArgs>
Public Custom Event ItemDataBound As EventHandler(Of ListViewItemEventArgs)
事件類型
範例
下列範例顯示如何使用 ItemDataBound 事件。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// <Snippet2>
protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label EmailAddressLabel;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// Display the email address in italics.
EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
EmailAddressLabel.Font.Italic = true;
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
string currentEmailAddress = rowView["EmailAddress"].ToString();
if (currentEmailAddress == "orlando0@adventure-works.com")
{
EmailAddressLabel.Font.Bold = true;
}
}
}
// </Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ListView ItemDataBound Example</title>
</head>
<body style="font: 10pt Trebuchet MS">
<form id="form1" runat="server">
<h3>
ListView ItemDataBound Example</h3>
<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" ConvertEmptyStringToNull="true"
OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
<LayoutTemplate>
<table cellpadding="2" width="680px" border="0">
<tr style="background-color: #ADD8E6" runat="server">
<th runat="server">
First Name
</th>
<th runat="server">
Last Name
</th>
<th runat="server">
Email Address
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #CAEEFF" runat="server">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</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="ContactsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT FirstName, LastName, EmailAddress FROM SalesLT.Customer">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <Snippet2>
Protected Sub ContactsListView_ItemDataBound(ByVal sender As Object, _
ByVal e As ListViewItemEventArgs)
If e.Item.ItemType = ListViewItemType.DataItem Then
' Display the email address in italics.
Dim EmailAddressLabel As Label = _
CType(e.Item.FindControl("EmailAddressLabel"), Label)
EmailAddressLabel.Font.Italic = True
Dim rowView As System.Data.DataRowView
rowView = CType(e.Item.DataItem, System.Data.DataRowView)
Dim currentEmailAddress As String = rowView("EmailAddress").ToString()
If currentEmailAddress = "orlando0@adventure-works.com" Then
EmailAddressLabel.Font.Bold = True
End If
End If
End Sub
' </Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ListView ItemDataBound Example</title>
</head>
<body style="font: 10pt Trebuchet MS">
<form id="form1" runat="server">
<h3>
ListView ItemDataBound Example</h3>
<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" ConvertEmptyStringToNull="true"
OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
<layouttemplate>
<table cellpadding="2" width="680px" border="0">
<tr style="background-color: #ADD8E6" runat="server">
<th runat="server">First Name</th>
<th runat="server">Last Name</th>
<th runat="server">Email Address</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
</Fields>
</asp:DataPager>
</layouttemplate>
<itemtemplate>
<tr style="background-color: #CAEEFF" runat="server">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</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="ContactsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT FirstName, LastName, EmailAddress FROM SalesLT.Customer">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
ListView在可以轉譯控件之前,控件中的每個專案都必須系結至數據源中的記錄。 ItemDataBound當數據項系結至 控件中的數據ListView時,就會引發 事件。 (Data 專案是由 ListViewDataItem objects 表示。) 這可讓您在發生此事件時執行自定義例程,例如修改系結至專案的數據值。
ListViewItemEventArgs物件會傳遞至事件處理程式,可讓您存取所系結之項目的屬性。 若要存取專案中的特定控制件,請使用 FindControl 物件之 屬性ListViewItemEventArgs中包含的 Item 物件方法ListViewItem。 您可以使用 屬性來判斷哪些項目類型 (資料項、插入專案、空專案) 系結 ItemType 。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。