Share via


ObjectDataSource.InsertParameters 屬性

定義

取得包含 InsertMethod 屬性所使用參數的參數集合。

public:
 property System::Web::UI::WebControls::ParameterCollection ^ InsertParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection InsertParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.InsertParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property InsertParameters As ParameterCollection

屬性值

ParameterCollection

ParameterCollection,包含 InsertMethod 屬性識別之方法所使用的參數。

屬性

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何使用 ObjectDataSource 物件搭配商務物件和 DetailsView 控制項來插入資料。 第二個程式碼範例提供第一個程式碼範例中使用的 方法範例實 Insert 作。

下列程式碼範例示範如何使用 ObjectDataSource 控制項搭配商務物件和 DetailsView 控制項來插入資料。 一開始,會顯示 DetailsView 文字方塊,您可以在其中輸入新 NorthwindEmployee 記錄的資料,以及自動產生的 [ 插入 ] 按鈕。 將資料輸入控制項的 DetailsView 欄位之後,按一下 [ 插入 ] 按鈕。 屬性 InsertMethod 會識別執行插入作業的方法。

如果您按一下 [ 插入] 按鈕,則會使用 屬性所 InsertMethod 指定的 方法以及集合中指定的 InsertParameters 任何參數來執行作業。 在此程式碼範例中,集合中 InsertParameters 指定了一個對應至監督員識別碼的參數。 這是因為即使控制項 BoundField 的集合 DetailsViewRows 以 物件的形式顯示識別碼,它也會以字串的形式傳遞至 ObjectDataSource 控制項。 藉由將它明確新增至集合, InsertParameters 並將屬性設定 Int32 為 值,它將會由 ObjectDataSource 正確傳遞至 方法作為 Int32 ,而不是 Type 字串。

Insert執行作業時,會呼叫 屬性所識別 InsertMethod 的方法。 Insert如果 物件的 方法有包含參數的方法簽章, InsertParameters 集合必須包含名稱符合方法之方法簽章參數 Insert 的參數,才能順利完成。

重要

您應該驗證您從用戶端收到的任何參數值。 執行時間只會將參數值取代為 InsertMethod 屬性。

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Import namespace="Samples.AspNet.CS" %>
<%@ 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>
    <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"
          defaultmode="Insert" >
          <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>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Import namespace="Samples.AspNet.VB" %>
<%@ 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>
    <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"
          defaultmode="Insert" >
          <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>

下列程式碼範例提供上述程式碼範例所使用的 方法範例實 Insert 作。 方法 InsertNewEmployeeWrapper 會新增至 EmployeeLogic 類別概觀中 ObjectDataSource 提供的仲介層物件,讓物件能夠更輕鬆地在 ObjectDataSource Web 案例中使用控制項,而不會大幅重寫至實際的商務邏輯。

若要執行此範例,您必須擁有 NorthwindEmployee 類別概觀中 ObjectDataSource 提供的 類別。 此範例只會說明如何使用參數,將 連接到 ObjectDataSource 商務物件方法,以取得新資料庫記錄的資料。 此範例不會將記錄新增至資料庫,因為 Save 類別的 NorthwindEmployee 方法不包含更新資料庫的程式碼。

// 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."); }
}
' 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


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

備註

集合中包含的 InsertParameters 參數名稱和類型必須符合屬性簽章中 InsertMethod 參數的名稱和類型。 參數名稱區分大小寫。 使用提供 和 DetailsView 控制項等 GridView 參數的資料繫結控制項時, ObjectDataSource 控制項會自動將集合中明確指定的任何參數與資料繫結控制項所提供的參數合併。 這很重要,因為資料系結控制項一律會提供其參數做為 String 類型,而且如果方法簽章包含數值或日期類型,您必須在集合中 InsertParameters 明確包含具有正確類型的參數。 否則, ObjectDataSource 控制項會嘗試根據集合中參數所定義的型別來轉換參數。 如需詳細資訊,請參閱 搭配 ObjectDataSource 控制項使用參數

屬性 InsertParametersInsertParameters 擷取與 控制項相關聯 ObjectDataSource 之 所包含的 ObjectDataSourceView 屬性。

如需參數合併、物件存留期和方法解析的詳細資訊,請參閱 InsertMethod

適用於

另請參閱