DetailsViewInsertEventArgs クラス

定義

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

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

次のコード例では、イベントハンドラーに渡されたオブジェクトを DetailsViewInsertEventArgs 使用して、イベントの ItemInserting 挿入操作を取り消す方法を示します。


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

注釈

コントロール内DetailsViewの Insert ボタン (プロパティが "Insert" に設定されたボタンCommandName) がクリックされると、コントロールがレコードを挿入する前にイベントがDetailsView発生ItemInsertingします。 これにより、このイベントが発生するたびに、データ ソースに挿入する前にレコードの値を HTML エンコードするなど、カスタム ルーチンを実行するイベント ハンドラーを提供できます。

DetailsViewInsertEventArgsオブジェクトはイベント ハンドラーに渡されます。これにより、コントロールに送信される省略可能なコマンド引数の値をDetailsView決定し、挿入操作を取り消す必要があることを示すことができます。 コマンド引数の値を確認するには、 プロパティを CommandArgument 使用します。 挿入操作を取り消すには、 プロパティを Canceltrue設定します。 プロパティを使用して Values 、新しいレコードのフィールド値を読み取ったり変更したりすることもできます。

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

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

コンストラクター

DetailsViewInsertEventArgs(Object)

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

プロパティ

Cancel

イベントをキャンセルするかどうかを示す値を取得または設定します。

(継承元 CancelEventArgs)
CommandArgument

DetailsView コントロールに渡される、挿入操作用のコマンド引数を取得します。

Values

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

メソッド

Equals(Object)

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

(継承元 Object)
GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

適用対象

こちらもご覧ください