DetailsViewInsertedEventArgs クラス

定義

ItemInserted イベントのデータを提供します。

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

次のコード例では、イベントハンドラーItemInsertedに渡されたオブジェクトをDetailsViewInsertedEventArgs使用して、挿入操作中に例外が発生したかどうかを判断する方法を示します。


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

注釈

コントロール内DetailsViewの [挿入] ボタン (プロパティが "Insert" に設定されたボタンCommandName) がクリックされると、コントロールはイベントをDetailsView発生ItemInsertedさせますが、コントロールによってレコードが挿入されます。 これにより、このイベントが発生するたびに、挿入操作の結果を確認するなど、カスタム ルーチンを実行するイベント ハンドラーを提供できます。

DetailsViewInsertedEventArgsオブジェクトがイベント ハンドラーに渡されます。これにより、影響を受けるレコードの数と発生した可能性のある例外を特定できます。 挿入操作の影響を受けるレコードの数を確認するには、 プロパティを AffectedRows 使用します。 例外が Exception 発生したかどうかを判断するには、 プロパティを使用します。 また、 プロパティを設定 ExceptionHandled することで、イベント ハンドラーで例外が処理されたかどうかを示すこともできます。 挿入されたレコードの値にアクセスする必要がある場合は、 プロパティを使用します Values

既定では、コントロールは DetailsView 挿入操作の後に プロパティで DefaultMode 指定されたモードに戻ります。 コントロールを DetailsView 挿入モードのままにするには、 プロパティを KeepInInsertModetrue設定します。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

DetailsViewDeletedEventArgs クラスのインスタンスの初期プロパティ値一覧については、DetailsViewDeletedEventArgs コンストラクターに関するトピックを参照してください。

コンストラクター

DetailsViewInsertedEventArgs(Int32, Exception)

DetailsViewInsertedEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

AffectedRows

挿入操作の影響を受けた行の数を取得します。

Exception

挿入操作中に例外が発生した場合は、その例外を取得します。

ExceptionHandled

挿入操作中に発生した例外がイベント ハンドラーで処理されたかどうかを示す値を取得または設定します。

KeepInInsertMode

挿入操作の後で、DetailsView コントロールを挿入モードのまま維持するかどうかを示す値を取得または設定します。

Values

挿入されたレコードのフィールドの名前と値のペアを格納しているディクショナリを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください