ObjectDataSource.InsertParameters プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ObjectDataSource.InsertParameters プロパティの意味・解説 

ObjectDataSource.InsertParameters プロパティ

メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。

InsertMethod メソッドによって使用されるパラメータを格納するパラメータ コレクションを取得します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public ReadOnly Property
 InsertParameters As ParameterCollection
Dim instance As ObjectDataSource
Dim value As ParameterCollection

value = instance.InsertParameters
public ParameterCollection InsertParameters { get;
 }
public:
property ParameterCollection^ InsertParameters {
    ParameterCollection^ get ();
}
/** @property */
public ParameterCollection get_InsertParameters ()
public function get InsertParameters
 () : ParameterCollection

プロパティ値
InsertMethod プロパティで識別されたメソッドによって使用されるパラメータを格納している ParameterCollection。

解説解説

InsertParameters コレクションに格納されているパラメータと InsertMethod プロパティ シグネチャのパラメータは、名前と型が一致している必要があります。パラメータ名では、大文字と小文字が区別されます。GridView コントロールや DetailsView コントロールなど、パラメータを提供するデータ バインド コントロールを使用する場合、ObjectDataSource コントロールは、コレクションで明示的に指定されたすべてのパラメータとデータ バインド コントロールによって提供されるパラメータを自動的にマージします。このことは、データ バインド コントロールがパラメータを常に String 型として提供し、メソッド シグネチャに数値型や日付型が含まれる場合は、InsertParameters コレクションに適切な型で明示的にパラメータを追加する必要があるという点で重要です。それ以外の場合、ObjectDataSource コントロールは、コレクション内のパラメータで定義された型に基づいてパラメータをキャストします。詳細については、「ObjectDataSource コントロールにおけるパラメータの使用」を参照してください。

InsertParameters プロパティは、ObjectDataSource コントロールに関連付けられた ObjectDataSourceView が格納する InsertParameters プロパティを取得します。

パラメータのマージ、オブジェクトの有効期間、およびメソッド解決の詳細については、InsertMethod のトピックを参照してください。

使用例使用例

このセクションには、2 つのコード例が含まれています。1 つ目のコード例では、ビジネス オブジェクトおよび DetailsView コントロールで、ObjectDataSource オブジェクトを使用してデータを挿入する方法を示します。2 つ目のコード例では、1 つ目のコード例で使用されている Insert メソッドの実装の例を示します。

ビジネス オブジェクトおよび DetailsView コントロールで、ObjectDataSource コントロールを使用してデータを挿入する方法を次のコード例に示します。まず、DetailsView は、新しい NorthwindEmployee レコードと自動生成された [挿入] ボタンを表示します。DetailsView コントロールのフィールドにデータを入力した後、[挿入] ボタンをクリックします。InsertMethod プロパティは、挿入操作を実行するメソッドを識別します。

[挿入] ボタンをクリックすると、InsertMethod プロパティで指定されたメソッド、および InsertParameters コレクションで指定された任意のパラメータを使用して操作が実行されます。このコード例では、管理者の ID に対応するパラメータが InsertParameters コレクションで指定されています。これは、ID が DetailsView コントロールの Rows コレクションに BoundField オブジェクトとして表示される場合でも、ID は文字列として ObjectDataSource コントロールに渡されるためです。Type プロパティを Int32 値に設定して、これを InsertParameters コレクションに明示的に追加することで、ObjectDataSource によって文字列ではなく Int32 として正しくメソッドに渡されます。

Insert 操作を実行すると、InsertMethod プロパティで識別されたメソッドが呼び出されます。オブジェクトの Insert メソッドがパラメータを含むメソッド シグネチャを持っている場合、Insert メソッドを正常に完了するには、InsertParameters コレクションにメソッド シグネチャ パラメータと一致する名前のパラメータが含まれている必要があります。

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB"
 Assembly="Samples.AspNet.VB"
 %>
<%@ Import namespace="Samples.AspNet.VB"
 %>
<%@ Page language="vb" %>
<html>
  <head>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post"
 runat="server">

        <asp:detailsview
          id="DetailsView1"
          runat="server"
          autogenerateinsertbutton="True"
          autogeneraterows="false"
          datasourceid="ObjectDataSource1" >
          <fields>
            <asp:BoundField headertext="FirstName"
 datafield="FirstName" />
            <asp:BoundField headertext="LastName"
   datafield="LastName" />
            <asp:BoundField headertext="Title"
      datafield="Title" />
            <asp:BoundField headertext="Courtesy"
   datafield="Courtesy" />
            <asp:BoundField headertext="Supervisor"
 datafield="Supervisor" />
          </fields>
        </asp:detailsview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetEmployee"
          insertmethod="InsertNewEmployeeWrapper"
          typename="Samples.AspNet.VB.EmployeeLogic"
 >
          <selectparameters>
            <asp:parameter name="anID" defaultvalue="-1"
 />
          </selectparameters>
          <insertparameters>
            <asp:parameter name="Supervisor" type="Int32"
 />
          </insertparameters>
        </asp:objectdatasource>

    </form>
  </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS"
 Assembly="Samples.AspNet.CS" %>
<%@ Import namespace="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<html>
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:detailsview
          id="DetailsView1"
          runat="server"
          autogenerateinsertbutton="True"
          autogeneraterows="false"
          datasourceid="ObjectDataSource1" >
          <fields>
            <asp:BoundField headertext="FirstName" datafield="FirstName"
 />
            <asp:BoundField headertext="LastName"   datafield="LastName"
 />
            <asp:BoundField headertext="Title"      datafield="Title"
 />
            <asp:BoundField headertext="Courtesy"   datafield="Courtesy"
 />
            <asp:BoundField headertext="Supervisor" datafield="Supervisor"
 />
          </fields>
        </asp:detailsview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetEmployee"
          insertmethod="InsertNewEmployeeWrapper"
          typename="Samples.AspNet.CS.EmployeeLogic" >
          <selectparameters>
            <asp:parameter name="anID" defaultvalue="-1" />
          </selectparameters>
          <insertparameters>
            <asp:parameter name="Supervisor" type="Int32"
 />
          </insertparameters>
        </asp:objectdatasource>

    </form>
  </body>
</html>

前のコード例で使用した Insert メソッドの実装の例を次のコード例に示します。InsertNewEmployeeWrapper メソッドを EmployeeLogic の中間層オブジェクトに追加することによって、実際のビジネス ロジックを大幅に書き換えることなく、Web シナリオでオブジェクトが ObjectDataSource コントロールをより簡単に使用できるようになります。

' This InsertNewEmployeeWrapper method is a wrapper method that enables
' the use of ObjectDataSource and InsertParameters, without
' substantially rewriting the true implementation for the NorthwindEmployee
' or the EmployeeLogic objects.
'
' The parameters to the method must be named the same as the
' DataControlFields used by the GridView or DetailsView controls.
Public Shared Sub InsertNewEmployeeWrapper(FirstName
 As String, LastName As
 String, Title As String, Courtesy As String, Supervisor As Integer)
   ' Build the NorthwindEmployee object and
   ' call the true  implementation.
   Dim tempEmployee As New
 NorthwindEmployee()

   tempEmployee.FirstName = FirstName
   tempEmployee.LastName = LastName
   tempEmployee.Title = Title
   tempEmployee.Courtesy = Courtesy
   tempEmployee.Supervisor = Supervisor

   ' Call the true implementation.
   InsertNewEmployee(tempEmployee)
End Sub 'InsertNewEmployeeWrapper


Public Shared Sub InsertNewEmployee(ne
 As NorthwindEmployee)
   Dim retval As Boolean
 = ne.Save()
   If Not retval Then
      Throw New NorthwindDataException("InsertNewEmployee
 failed.")
   End If
End Sub 'InsertNewEmployee
// This InsertNewEmployeeWrapper method is a wrapper method that enables
// the use of ObjectDataSource and InsertParameters, without
// substantially rewriting the true implementation for the NorthwindEmployee
// or the EmployeeLogic objects.
//
// The parameters to the method must be named the same as the
// DataControlFields used by the GridView or DetailsView controls.
public static void InsertNewEmployeeWrapper
 (string FirstName,
                                             string LastName,
                                             string Title,
                                             string Courtesy,
                                             int    Supervisor)
{
  // Build the NorthwindEmployee object and
  // call the true  implementation.
  NorthwindEmployee tempEmployee = new NorthwindEmployee();

  tempEmployee.FirstName  = FirstName;
  tempEmployee.LastName   = LastName;
  tempEmployee.Title      = Title;
  tempEmployee.Courtesy   = Courtesy;
  tempEmployee.Supervisor = Supervisor;

  // Call the true implementation.
  InsertNewEmployee(tempEmployee);
}

public static void InsertNewEmployee(NorthwindEmployee
 ne) {
  bool retval = ne.Save();
  if (! retval) { throw new NorthwindDataException("InsertNewEmployee
 failed."); }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ObjectDataSource クラス
ObjectDataSource メンバ
System.Web.UI.WebControls 名前空間
ObjectDataSource.InsertMethod プロパティ
Insert



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ObjectDataSource.InsertParameters プロパティ」の関連用語

ObjectDataSource.InsertParameters プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ObjectDataSource.InsertParameters プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS