DetailsView.DataItem 属性

定义

获取绑定到 DetailsView 控件的数据项。Gets the data item bound to the DetailsView control.

public:
 virtual property System::Object ^ DataItem { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual object DataItem { get; }
member this.DataItem : obj
Public Overridable ReadOnly Property DataItem As Object

属性值

一个 Object,表示绑定到 DetailsView 控件的数据项。An Object that represents the data item bound to the DetailsView control.

实现

属性

示例

下面的代码示例演示如何使用DataItem属性来确定数据项中的字段的值。The following code example demonstrates how to use the DataItem property to determine the value of a field in the data item.


<%@ 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>

注解

使用属性可访问绑定DetailsView到控件的数据项。 DataItemUse the DataItem property to access the data item bound to the DetailsView control. 该数据项通常用于直接访问当前记录的字段值。The data item is often used to access the field values of the current record directly. 此属性Object返回的类型因数据源而异。The type of the Object returned by this property varies depending on the data source. 例如, 将SqlDataSource控件绑定DetailsView到控件时, 将返回一个DataRowView对象。For example, when a SqlDataSource control is bound to the DetailsView control, a DataRowView object is returned.

备注

此属性仅在数据绑定发生之后才可用。This property is available only after data binding has occurred. 数据绑定发生之前, 此属性返回nullBefore data binding occurs, this property returns null. DetailsView控件处于插入模式并返回null时, 此属性也不适用。This property is also not applicable when the DetailsView control is in insert mode and returns null.

适用于

另请参阅