Share via


DetailsViewInsertedEventArgs Kelas

Definisi

Menyediakan data untuk peristiwa tersebut ItemInserted .

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

Contoh

Contoh kode berikut menunjukkan cara menggunakan objek yang DetailsViewInsertedEventArgs diteruskan ke penanganan aktivitas untuk peristiwa guna ItemInserted menentukan apakah pengecualian terjadi selama operasi penyisipan.


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

Keterangan

DetailsView Kontrol menaikkan ItemInserted peristiwa ketika tombol Sisipkan (tombol dengan propertinya CommandName diatur ke "Sisipkan") di dalam kontrol diklik, tetapi setelah DetailsView kontrol menyisipkan rekaman. Ini memungkinkan Anda untuk menyediakan penanganan aktivitas yang melakukan rutinitas kustom, seperti memeriksa hasil operasi penyisipan, setiap kali peristiwa ini terjadi.

Objek DetailsViewInsertedEventArgs diteruskan ke penanganan aktivitas, yang memungkinkan Anda menentukan jumlah rekaman yang terpengaruh dan pengecualian apa pun yang mungkin telah terjadi. Untuk menentukan jumlah rekaman yang terpengaruh oleh operasi sisipkan, gunakan AffectedRows properti . Exception Gunakan properti untuk menentukan apakah ada pengecualian yang terjadi. Anda juga dapat menunjukkan apakah pengecualian ditangani di penanganan aktivitas dengan mengatur ExceptionHandled properti . Jika Anda perlu mengakses nilai rekaman yang disisipkan, gunakan Values properti .

Secara default, DetailsView kontrol kembali ke mode yang ditentukan oleh DefaultMode properti setelah operasi penyisipan. Untuk mempertahankan DetailsView kontrol dalam mode sisipkan, atur properti ke KeepInInsertModetrue.

Untuk informasi selengkapnya tentang cara menangani peristiwa, lihat Menangani dan Menaikkan Peristiwa.

Untuk daftar nilai properti awal untuk instans DetailsViewDeletedEventArgs kelas , lihat DetailsViewDeletedEventArgs konstruktor .

Konstruktor

DetailsViewInsertedEventArgs(Int32, Exception)

Menginisialisasi instans baru kelas DetailsViewInsertedEventArgs.

Properti

AffectedRows

Mendapatkan jumlah baris yang terpengaruh oleh operasi sisipkan.

Exception

Mendapatkan pengecualian (jika ada) yang dinaikkan selama operasi penyisipan.

ExceptionHandled

Mendapatkan atau menetapkan nilai yang menunjukkan apakah pengecualian yang dinaikkan selama operasi penyisipan ditangani di penanganan aktivitas.

KeepInInsertMode

Mendapatkan atau mengatur nilai yang menunjukkan apakah DetailsView kontrol harus tetap berada dalam mode sisipkan setelah operasi sisipkan.

Values

Mendapatkan kamus yang berisi pasangan nama bidang/nilai untuk rekaman yang disisipkan.

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Lihat juga