DetailsView.DataItem 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得繫結至 DetailsView 控制項的資料項目。
public:
virtual property System::Object ^ DataItem { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual object DataItem { get; }
[<System.ComponentModel.Browsable(false)>]
member this.DataItem : obj
Public Overridable ReadOnly Property DataItem As Object
屬性值
Object,表示繫結至 DetailsView 控制項的資料項目。
實作
- 屬性
範例
下列程式碼範例示範如何使用 DataItem 屬性來判斷資料項目中欄位的值。
<%@ Page language="C#" %>
<%@ import namespace="System.Data"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void CustomerDetailView_DataBound(object sender,
EventArgs e)
{
DataRowView rowView = (DataRowView)CustomerDetailView.DataItem;
if (rowView.Row[0].ToString() == "SpecialID")
{
CustomerDetailView.FieldHeaderStyle.BackColor =
System.Drawing.Color.Red;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ModeChanging Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ModeChanging Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
autogenerateeditbutton="true"
OnDataBound="CustomerDetailView_DataBound"
allowpaging="true"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<%@ import namespace="System.Data"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub CustomerDetailView_DataBound( _
ByVal sender As Object, ByVal e As EventArgs)
Dim rowView As DataRowView = _
CType(CustomerDetailView.DataItem, DataRowView)
If rowView.Row(0).ToString() = "SpecialID" Then
CustomerDetailView.FieldHeaderStyle.BackColor = _
System.Drawing.Color.Red
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ModeChanging Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ModeChanging Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
autogenerateeditbutton="true"
OnDataBound="CustomerDetailView_DataBound"
allowpaging="true"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
DataItem使用 屬性來存取系結至 DetailsView 控制項的資料項目。 資料項目通常用來直接存取目前記錄的域值。 這個屬性所傳回的 Object 型別會根據資料來源而有所不同。 例如,當控制項系結至 DetailsView 控制項時 SqlDataSource , DataRowView 會傳回 物件。
注意
只有在發生資料系結之後,才能使用這個屬性。 在資料系結髮生之前,這個屬性會傳 null
回 。 當控制項處於插入模式並傳回 null
時 DetailsView ,此屬性也不適用。