DetailsViewInsertEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia ItemInserting.

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

Przykłady

W poniższym przykładzie kodu pokazano, jak używać obiektu przekazanego DetailsViewInsertEventArgs do programu obsługi zdarzeń dla ItemInserting zdarzenia w celu anulowania operacji wstawiania.


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

Uwagi

Kontrolka DetailsViewItemInserting zgłasza zdarzenie, gdy przycisk Wstaw (przycisk z jego CommandName właściwością ustawioną na "Wstaw") w kontrolce jest klikany, ale zanim DetailsView kontrolka wstawi rekord. Dzięki temu można udostępnić procedurę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak kodowanie HTML wartości rekordu przed wstawieniem go w źródle danych, przy każdym wystąpieniu tego zdarzenia.

DetailsViewInsertEventArgs Obiekt jest przekazywany do procedury obsługi zdarzeń, co pozwala określić wartość opcjonalnego argumentu polecenia wysyłanego do kontrolki DetailsView i wskazać, że operacja wstawiania powinna zostać anulowana. Aby określić wartość argumentu polecenia, użyj CommandArgument właściwości . Aby anulować operację wstawiania, ustaw Cancel właściwość na true. Możesz również odczytywać lub modyfikować wartości pól dla nowego rekordu Values przy użyciu właściwości .

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Aby uzyskać listę początkowych wartości właściwości dla wystąpienia DetailsViewInsertEventArgs klasy, zobacz DetailsViewInsertEventArgs konstruktor.

Konstruktory

DetailsViewInsertEventArgs(Object)

Inicjuje nowe wystąpienie klasy DetailsViewInsertEventArgs.

Właściwości

Cancel

Pobiera lub ustawia wartość wskazującą, czy zdarzenie powinno zostać anulowane.

(Odziedziczone po CancelEventArgs)
CommandArgument

Pobiera argument polecenia dla operacji wstawiania przekazanej do kontrolki DetailsView .

Values

Pobiera słownik zawierający pary nazwa/wartość pola do wstawienia rekordu.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też