DetailsView.AutoGenerateInsertButton プロパティ

定義

新しいレコードを挿入するための組み込みのコントロールを DetailsView コントロールに表示するかどうかを示す値を取得または設定します。

public:
 virtual property bool AutoGenerateInsertButton { bool get(); void set(bool value); };
public virtual bool AutoGenerateInsertButton { get; set; }
member this.AutoGenerateInsertButton : bool with get, set
Public Overridable Property AutoGenerateInsertButton As Boolean

プロパティ値

Boolean

新しいレコードを挿入するための組み込みのコントロールを表示する場合は true。それ以外の場合は false。 既定値は、false です。

次のコード例では、このプロパティを使用 AutoGenerateInsertButton して組み込みコントロールを表示し、コントロールに新しいレコードを挿入する方法を DetailsView 示します。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] 
            From [Customers]">
        </asp:SqlDataSource>
    </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] 
            From [Customers]">
        </asp:SqlDataSource>
    </form>
  </body>
</html>

注釈

挿入をサポートするデータ ソース コントロールがコントロールに DetailsView バインドされている場合、 DetailsView コントロールはデータ ソース コントロールの機能を利用し、自動挿入機能を提供できます。

注意

データ ソース コントロールでデータを挿入するには、そのプロパティを SqlDataSource.InsertCommand insert クエリ ステートメントで設定する必要があります。

プロパティを AutoGenerateInsertButton 設定 trueすると、 CommandField 新しいボタンを持つ行フィールドがコントロールに DetailsView 自動的に表示されます。 [新規] ボタンをクリックすると、そのコントロールが DetailsView 挿入モードになります。 挿入モードの場合、読み取り専用ではないコントロール内のバインドされた各フィールドには、フィールドのデータ型の適切な入力コントロール (コントロールなど TextBox ) が表示されます。 これにより、ユーザーは新しいレコードのフィールドの値を入力できます。

クリックすると、[新規] ボタンも [挿入] ボタンと [キャンセル] ボタンに置き換えられます。 [挿入] ボタンをクリックすると、データ ソースに新しいレコードが挿入され、プロパティで指定されたモードにコントロールが DefaultMode 返されます。 [キャンセル] ボタンをクリックすると、挿入操作が中止され、コントロールが既定のモードに戻ります。

注意

プログラムで行を挿入モードにするには、このメソッドを使用します ChangeMode

プロパティを使用して、挿入モードのレコードの外観を InsertRowStyle 制御できます。 一般的な設定には、通常、カスタム背景色、前景色、フォント プロパティが含まれます。

コントロールには DetailsView 、新しいレコードが挿入されたときにカスタム アクションを実行するために使用できるいくつかのイベントが用意されています。 次の表に、使用可能なイベントの一覧を示します。

Event 説明
ItemInserted [挿入] ボタンをクリックしたときに、コントロールがレコードを DetailsView 挿入した後に発生します。 このイベントは、多くの場合、挿入操作の結果を確認するために使用されます。
ItemInserting [挿入] ボタンをクリックしたときに、コントロールがレコードを挿入する DetailsView 前に発生します。 このイベントは、多くの場合、挿入操作を取り消すために使用されます。
ModeChanged コントロールがモードを変更した後に DetailsView 発生します。
ModeChanging コントロールがモードを変更する DetailsView 前に発生します。 このイベントは、多くの場合、モードの変更を取り消すために使用されます。

AutoGenerateInsertButton 値はビュー ステートに格納されます。

適用対象

こちらもご覧ください