DetailsView.DataItem Propriété

Définition

Obtient l'élément de données lié au contrôle 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

Valeur de propriété

Object qui représente un élément de données lié au contrôle DetailsView.

Implémente

Attributs

Exemples

L’exemple de code suivant montre comment utiliser la DataItem propriété pour déterminer la valeur d’un champ dans l’élément de données.


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

Remarques

Utilisez la DataItem propriété pour accéder à l’élément de données lié au DetailsView contrôle. L’élément de données est souvent utilisé pour accéder directement aux valeurs de champ de l’enregistrement actif. Le type du Object retourné par cette propriété varie en fonction de la source de données. Par exemple, lorsqu’un SqlDataSource contrôle est lié au DetailsView contrôle, un DataRowView objet est retourné.

Notes

Cette propriété est disponible uniquement après la liaison de données. Avant que la liaison de données ne se produise, cette propriété retourne null. Cette propriété n’est pas non plus applicable lorsque le DetailsView contrôle est en mode Insertion et retourne null.

S’applique à

Voir aussi