DetailsViewUpdatedEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia ItemUpdated.

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

Przykłady

Poniższy przykład kodu pokazuje, jak używać DetailsViewUpdatedEventArgs obiektu przekazanego do programu obsługi zdarzeń dla ItemUpdated zdarzenia w celu określenia, czy wystąpił wyjątek podczas operacji aktualizacji.


<%@ 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_ItemUpdated(Object sender, 
    DetailsViewUpdatedEventArgs e)
  {
    // Use the Exception property to determine whether an exception
    // occurred during the insert operation.
    if (e.Exception == null)
    {
      // Use the Values property to get the value entered by 
      // the user for the CompanyName field.
      String keyFieldValue = e.Keys["CustomerID"].ToString();

      // Display a confirmation message.
      MessageLabel.Text = "Record " + keyFieldValue + 
        " updated successfully. ";

      // Display the old and new values.
      DisplayValues(e);

      if (e.AffectedRows == 1)
      {
        MessageLabel.Text += e.AffectedRows.ToString() + 
          " record updated.";
      }
      else
      {
        MessageLabel.Text += e.AffectedRows.ToString() + 
          " records updated.";
      }
    }
    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 edit mode.
      e.KeepInEditMode = true;
    }
  }

  void DisplayValues(DetailsViewUpdatedEventArgs e)
  {
    
    MessageLabel.Text += "<br/></br>";
    
    // Iterate through the OldValue and NewValues
    // properties and display the values.
    for (int i = 0; i < e.OldValues.Count; i++)
    {
      MessageLabel.Text += "Old Value=" + e.OldValues[i].ToString() +
        ", New Value=" + e.NewValues[i].ToString() + "<br/>";
    }

    MessageLabel.Text += "</br>";
    
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DetailsViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewUpdatedEventArgs Example</h3>
                       
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogenerateeditbutton="true"  
          allowpaging="true"
          datakeynames="CustomerID" 
          onitemupdated="CustomerDetailsView_ItemUpdated"
          runat="server">
            
          <pagersettings position="Bottom"/> 
                    
        </asp:detailsview>
        
        <br/>
        
        <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]"
          updatecommand="Update [Customers] Set 
          [CompanyName]=@CompanyName, [Address]=@Address, 
          [City]=@City, [PostalCode]=@PostalCode, 
          [Country]=@Country 
          Where [CustomerID]=@CustomerID"
          connectionstring=
          "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

<%@ Page language="VB" %>

<!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_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
  
    ' Use the Exception property to determine whether an exception
    ' occurred during the insert operation.
    If e.Exception Is Nothing Then
    
      ' Use the Values property to get the value entered by 
      ' the user for the CompanyName field.
      Dim keyFieldValue As String = e.Keys("CustomerID").ToString()

      ' Display a confirmation message.
      MessageLabel.Text = "Record " & keyFieldValue & _
        " updated successfully. "

      ' Display the old and new values.
      DisplayValues(e)

      If e.AffectedRows = 1 Then

        MessageLabel.Text &= e.AffectedRows.ToString() & _
          " record updated."
      
      Else
      
        MessageLabel.Text &= e.AffectedRows.ToString() & _
          " records updated."
      
      End If
    
    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 edit mode.
      e.KeepInEditMode = True
    
    End If
    
  End Sub

  Sub DisplayValues(ByVal e As DetailsViewUpdatedEventArgs)
    
    MessageLabel.Text &= "<br/></br>"
    
    ' Iterate through the OldValue and NewValues
    ' properties and display the values.
    Dim i As Integer
        
    For i = 0 To e.OldValues.Count - 1
    
      MessageLabel.Text &= "Old Value=" & e.OldValues(i).ToString() & _
        ", New Value=" & e.NewValues(i).ToString() & "<br/>"
    
    Next

    MessageLabel.Text &= "</br>"
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DetailsViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewUpdatedEventArgs Example</h3>
                       
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogenerateeditbutton="true"  
          allowpaging="true"
          datakeynames="CustomerID" 
          onitemupdated="CustomerDetailsView_ItemUpdated"
          runat="server">
            
          <pagersettings position="Bottom"/> 
                    
        </asp:detailsview>
        
        <br/>
        
        <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]"
          updatecommand="Update [Customers] Set 
          [CompanyName]=@CompanyName, [Address]=@Address, 
          [City]=@City, [PostalCode]=@PostalCode, 
          [Country]=@Country 
          Where [CustomerID]=@CustomerID"
          connectionstring=
          "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

Uwagi

Kontrolka DetailsView zgłasza ItemUpdated zdarzenie, gdy przycisk Aktualizuj (przycisk z właściwością CommandName ustawioną na "Aktualizuj") w kontrolce jest klikany, ale po zaktualizowaniu rekordu DetailsView kontrolki. Dzięki temu można podać procedurę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak sprawdzanie wyników operacji aktualizacji, za każdym razem, gdy wystąpi to zdarzenie.

DetailsViewUpdatedEventArgs 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 aktualizacji, 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 pól klucza dla zaktualizowanego rekordu, użyj Keys właściwości . Dostęp do oryginalnych i zaktualizowanych wartości pól innych niż klucz można uzyskać, odpowiednio przy użyciu OldValues właściwości i NewValues .

Domyślnie kontrolka DetailsView powraca do trybu określonego DefaultMode przez właściwość po operacji aktualizacji. Aby zachować kontrolkę DetailsView w trybie edycji, ustaw KeepInEditMode 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 DetailsViewUpdatedEventArgs klasy, zobacz DetailsViewUpdatedEventArgs konstruktor.

Konstruktory

DetailsViewUpdatedEventArgs(Int32, Exception)

Inicjuje nowe wystąpienie klasy DetailsViewUpdatedEventArgs.

Właściwości

AffectedRows

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

Exception

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

ExceptionHandled

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

KeepInEditMode

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

Keys

Pobiera słownik zawierający pary nazwa/wartość pola klucza dla zaktualizowanego rekordu.

NewValues

Pobiera słownik zawierający nowe pary nazwy/wartości pola dla zaktualizowanego rekordu.

OldValues

Pobiera słownik zawierający oryginalne pary nazwy/wartości pola dla zaktualizowanego 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ż