Export (0) Print
Expand All
Expand Minimize

DetailsView.DataItem Property

Gets the data item bound to the DetailsView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[BrowsableAttribute(false)]
public virtual Object DataItem { get; }

Property Value

Type: System.Object
An Object that represents the data item bound to the DetailsView control.

Implements

IDataItemContainer.DataItem

Use 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. The type of the Object returned by this property varies depending on the data source. For example, when a SqlDataSource control is bound to the DetailsView control, a DataRowView object is returned.

NoteNote

This property is available only after data binding has occurred. Before data binding occurs, this property returns null. This property is also not applicable when the DetailsView control is in insert mode and returns null.

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>

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft