DetailsViewInsertEventArgs Classe

Definizione

Fornisce i dati per l'evento ItemInserting.

public ref class DetailsViewInsertEventArgs : System::ComponentModel::CancelEventArgs
public class DetailsViewInsertEventArgs : System.ComponentModel.CancelEventArgs
type DetailsViewInsertEventArgs = class
    inherit CancelEventArgs
Public Class DetailsViewInsertEventArgs
Inherits CancelEventArgs
Ereditarietà
DetailsViewInsertEventArgs

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare l'oggetto DetailsViewInsertEventArgs passato al gestore eventi per l'evento ItemInserting per annullare un'operazione di inserimento.


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

  void CustomerDetailsView_ItemInserting(Object sender, 
    DetailsViewInsertEventArgs e)
  {
    // Use the Values property to retrieve the key field value.
    String keyValue = e.Values["CustomerID"].ToString();

    // Insert the record only if the key field is four characters
    // long; otherwise, cancel the insert operation.
    if (keyValue.Length == 4)
    {
      // Change the key field value to upper case before inserting 
      // the record in the data source.
      e.Values["CustomerID"] = keyValue.ToUpper();

      MessageLabel.Text = "";
    }
    else
    {
      MessageLabel.Text = "The key field must have four digits.";
      e.Cancel = true;
    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewInsertEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewInsertEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          oniteminserting="CustomerDetailsView_ItemInserting" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- 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"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          insertcommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], 
            [Country]) VALUES (@CustomerID, @CompanyName, @Address, 
            @City, @PostalCode, @Country)"
          connectionstring=
          "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

<%@ Page language="VB" autoeventwireup="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomerDetailsView_ItemInserting(ByVal sender As Object, _
    ByVal e As DetailsViewInsertEventArgs) _
    Handles CustomerDetailsView.ItemInserting
  
    ' Use the Values property to retrieve the key field value.
    Dim keyValue As String = e.Values("CustomerID").ToString()

    ' Insert the record only if the key field is four characters
    ' long; otherwise, cancel the insert operation.
    If keyValue.Length = 4 Then
    
      ' Change the key field value to upper case before inserting 
      ' the record in the data source.
      e.Values("CustomerID") = keyValue.ToUpper()
      
      MessageLabel.Text = ""
    
    Else
    
      MessageLabel.Text = "The key field must have four digits."
      e.Cancel = True
    
    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewInsertEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewInsertEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          oniteminserting="CustomerDetailsView_ItemInserting" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- 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"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          insertcommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], 
            [Country]) VALUES (@CustomerID, @CompanyName, @Address, 
            @City, @PostalCode, @Country)"
          connectionstring=
          "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

Commenti

Il DetailsView controllo genera l'evento ItemInserting quando viene fatto clic su un pulsante Inserisci (un pulsante con la relativa CommandName proprietà impostata su "Inserisci") all'interno del controllo, ma prima che il DetailsView controllo inserisca il record. In questo modo è possibile fornire un gestore eventi che esegue una routine personalizzata, ad esempio la codifica HTML dei valori di un record prima di inserirlo nell'origine dati, ogni volta che si verifica questo evento.

Un DetailsViewInsertEventArgs oggetto viene passato al gestore eventi, che consente di determinare il valore di un argomento di comando facoltativo inviato al DetailsView controllo e di indicare che l'operazione di inserimento deve essere annullata. Per determinare il valore dell'argomento del comando, utilizzare la CommandArgument proprietà . Per annullare l'operazione di inserimento, impostare la Cancel proprietà su true. È anche possibile leggere o modificare i valori dei campi per il nuovo record usando la Values proprietà .

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.

Per un elenco dei valori iniziali delle proprietà di un'istanza della classe DetailsViewInsertEventArgs, vedere il costruttore DetailsViewInsertEventArgs.

Costruttori

DetailsViewInsertEventArgs(Object)

Inizializza una nuova istanza della classe DetailsViewInsertEventArgs.

Proprietà

Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.

(Ereditato da CancelEventArgs)
CommandArgument

Ottiene l'argomento di comando per l'operazione di inserimento passato al controllo DetailsView.

Values

Ottiene un dizionario contenente le coppie nome/valore dei campi per il record da inserire.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche