DetailsViewInsertedEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia ItemInserted.

public ref class DetailsViewInsertedEventArgs : EventArgs
public class DetailsViewInsertedEventArgs : EventArgs
type DetailsViewInsertedEventArgs = class
    inherit EventArgs
Public Class DetailsViewInsertedEventArgs
Inherits EventArgs
Dziedziczenie
DetailsViewInsertedEventArgs

Przykłady

Poniższy przykład kodu pokazuje, jak używać DetailsViewInsertedEventArgs obiektu przekazanego do procedury obsługi zdarzeń dla ItemInserted zdarzenia w celu określenia, czy wystąpił wyjątek podczas 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_ItemInserted(Object sender, 
    DetailsViewInsertedEventArgs e)
  {
    // Use the Exception property to determine whether an exception
    // occurred during the insert operation.
    if (e.Exception == null && e.AffectedRows == 1)
    {
      // Use the Values property to get the value entered by 
      // the user for the CompanyName field.
      String name = e.Values["CompanyName"].ToString();

      // Display a confirmation message.
      MessageLabel.Text = name + " added successfully. ";

    }
    else
    {
      // Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message;
      
      // Use the ExceptionHandled property to indicate that the 
      // exception is already handled.
      e.ExceptionHandled = true;
      
      // When an exception occurs, keep the DetailsView
      // control in insert mode.
      e.KeepInInsertMode = true;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewInsertedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewInsertedEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          oniteminserted="CustomerDetailsView_ItemInserted" 
          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_ItemInserted(ByVal sender As Object, _
    ByVal e As DetailsViewInsertedEventArgs) _
    Handles CustomerDetailsView.ItemInserted

    ' Use the Exception property to determine whether an exception
    ' occurred during the insert operation.
    If e.Exception Is Nothing And e.AffectedRows = 1 Then
    
      ' Use the Values property to get the value entered by 
      ' the user for the CompanyName field.
      Dim name As String = e.Values("CompanyName").ToString()

      ' Display a confirmation message.
      MessageLabel.Text = name & " added successfully. "
    
    Else
    
      ' Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message
      
      ' Use the ExceptionHandled property to indicate that the 
      ' exception is already handled.
      e.ExceptionHandled = True
      
      ' When an exception occurs, keep the DetailsView
      ' control in insert mode.
      e.KeepInInsertMode = True
    
    End If
        
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewInsertedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewInsertedEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          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 DetailsView zgłasza ItemInserted zdarzenie, gdy przycisk Wstaw (przycisk z właściwością CommandName ustawioną na "Wstaw") w kontrolce jest klikany, ale po DetailsView wstawieniu rekordu kontrolki. Dzięki temu można udostępnić procedurę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak sprawdzanie wyników operacji wstawiania, za każdym razem, gdy wystąpi to zdarzenie.

DetailsViewInsertedEventArgs Obiekt jest przekazywany do programu obsługi zdarzeń, co pozwala określić liczbę rekordów, których dotyczy problem, i wszelkie wyjątki, które mogły wystąpić. Aby określić liczbę rekordów, których dotyczy operacja wstawiania, użyj AffectedRows właściwości . Użyj właściwości , Exception aby określić, czy wystąpiły jakiekolwiek wyjątki. Możesz również wskazać, czy wyjątek został obsłużony w procedurze obsługi zdarzeń, ustawiając ExceptionHandled właściwość. Jeśli musisz uzyskać dostęp do wartości wstawionego rekordu, użyj Values właściwości .

Domyślnie kontrolka DetailsView powraca do trybu określonego DefaultMode przez właściwość po operacji wstawiania. Aby zachować kontrolkę DetailsView w trybie wstawiania, ustaw KeepInInsertMode właściwość na true.

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 DetailsViewDeletedEventArgs klasy, zobacz DetailsViewDeletedEventArgs konstruktor.

Konstruktory

DetailsViewInsertedEventArgs(Int32, Exception)

Inicjuje nowe wystąpienie klasy DetailsViewInsertedEventArgs.

Właściwości

AffectedRows

Pobiera liczbę wierszy, których dotyczy operacja wstawiania.

Exception

Pobiera wyjątek (jeśli istnieje) zgłoszony podczas operacji wstawiania.

ExceptionHandled

Pobiera lub ustawia wartość wskazującą, czy wyjątek zgłoszony podczas operacji wstawiania został obsłużony w procedurze obsługi zdarzeń.

KeepInInsertMode

Pobiera lub ustawia wartość wskazującą, czy kontrolka DetailsView powinna pozostać w trybie wstawiania po operacji wstawiania.

Values

Pobiera słownik zawierający pary nazwa/wartość pola dla wstawionego 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 bieżące wystąpienie.

(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ż