ObjectDataSource.InsertParameters ObjectDataSource.InsertParameters ObjectDataSource.InsertParameters ObjectDataSource.InsertParameters Property

定义

获取参数集合,该集合包含由 InsertMethod 属性使用的参数。Gets the parameters collection that contains the parameters that are used by the InsertMethod property.

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; }
member this.InsertParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property InsertParameters As ParameterCollection

属性值

包含由 ParameterCollection 属性标识的方法所使用的参数的 InsertMethodA ParameterCollection that contains the parameters used by the method identified by the InsertMethod property.

示例

本部分包含两个代码示例。This section contains two code examples. 第一个代码示例演示如何将ObjectDataSource对象与业务对象DetailsView和控件结合使用来插入数据。The first code example demonstrates how to use an ObjectDataSource object with a business object and a DetailsView control to insert data. 第二个代码示例提供了在第一个Insert代码示例中使用的方法的示例实现。The second code example provides an example implementation of the Insert method that is used in the first code example.

下面的代码示例演示如何将ObjectDataSource控件与业务对象DetailsView和控件结合使用来插入数据。The following code example demonstrates how to use an ObjectDataSource control with a business object and a DetailsView control to insert data. 最初, DetailsView会显示文本框, 您可以在其中为新NorthwindEmployee记录输入数据, 以及自动生成的 "插入" 按钮。Initially, the DetailsView displays text boxes in which you can enter data for a new NorthwindEmployee record, along with an automatically generated Insert button. DetailsView控件的字段中输入数据后, 单击 "插入" 按钮。After you enter data into the fields of the DetailsView control, click the Insert button. InsertMethod属性标识执行插入操作的方法。The InsertMethod property identifies which method performs the insert operation.

如果单击 "插入" 按钮, 则将使用由InsertMethod属性指定的方法和InsertParameters集合中指定的任何参数执行该操作。If you click the Insert button, the operation is performed using the method that is specified by the InsertMethod property and any parameters that are specified in the InsertParameters collection. 在此代码示例中, 在与主管 ID 相对InsertParameters应的集合中指定一个参数。In this code example, one parameter is specified in the InsertParameters collection that corresponds to the supervisor's ID. 这是Rows因为即使 ID 在DetailsView控件的集合中显示为BoundField对象, ObjectDataSource也会以字符串的形式传递到控件。This is because even though the ID is displayed in the Rows collection for the DetailsView control as a BoundField object, it is passed as a string to the ObjectDataSource control. 通过将其显式添加到InsertParameters Type具有设置为Int32值的属性的集合ObjectDataSource , 会将其作为Int32传递给方法, 而不是字符串。By adding it explicitly to the InsertParameters collection with a Type property set to the Int32 value, it will be passed correctly by the ObjectDataSource to the method as an Int32, not as a string.

执行操作时, 将调用InsertMethod由属性标识的方法。 InsertWhen the Insert operation is performed, the method that is identified by the InsertMethod property is called. 如果对象的InsertParameters Insert方法具有包含参数的方法签名, 则集合必须包含名称与方法签名参数相匹配的参数, 以使方法成功完成。 InsertIf the Insert method of the object has a method signature that includes parameters, the InsertParameters collection must contain parameters that have names that match the method signature parameters for the Insert method to complete successfully.

重要

你应验证从客户端接收的任何参数值。You should validate any parameter value that you receive from the client. 运行时只需将参数值替换为InsertMethod属性。The runtime simply substitutes the parameter value into the InsertMethod property.

<%@ 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使用的方法的示例实现。The following code example provides an example implementation of the Insert method that the preceding code example uses. 方法添加ObjectDataSource到了类概述中提供的ObjectDataSource 中间层对象,使对象能够更轻松地在Web方案中与控件一起工作,而不会对实际EmployeeLogic InsertNewEmployeeWrapper业务逻辑。The InsertNewEmployeeWrapper method is added to the EmployeeLogic middle-tier object that is provided in the ObjectDataSource class overview to enable the object to work more easily with the ObjectDataSource control in Web scenarios, without a substantial rewrite to the actual business logic.

若要运行此示例, 您必须具有NorthwindEmployee ObjectDataSource类概述中提供的类。To run the example, you must have the NorthwindEmployee class that is provided in the ObjectDataSource class overview. 此示例仅演示了如何使用参数ObjectDataSource将连接到用于获取新数据库记录的数据的业务对象方法。This example illustrates only how to connect the ObjectDataSource to a business object method that gets data for a new database record by using parameters. 此示例不会将记录添加到数据库, 因为Save NorthwindEmployee该类的方法不包括更新数据库的代码。The example does not add records to the database, because the Save method of the NorthwindEmployee class does not include code to update the database.

// 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属性签名中的参数的名称和类型相匹配。The names and types of the parameters that are contained in the InsertParameters collection must match the names and types of the parameters that are in the InsertMethod property signature. 参数名称区分大小写。The parameter names are case sensitive. 在使用提供参数的数据绑定控件 (如GridViewDetailsView控件) 时, ObjectDataSource控件自动将集合中显式指定的所有参数与的参数合并由数据绑定控件提供。When working with data-bound controls that supply parameters, such as the GridView and DetailsView controls, the ObjectDataSource control automatically merges any parameters that are explicitly specified in the collection with the parameters that are provided by the data-bound control. 这一点很重要, 因为数据绑定控件始终将其参数String作为类型提供, 并且如果方法签名包含数字或日期类型, 则必须InsertParameters在具有正确类型的集合中显式包含一个参数。This is important because data-bound controls always supply their parameters as String types, and if the method signature includes numeric or date types, you must explicitly include a parameter in the InsertParameters collection with the correct type. 否则, ObjectDataSource控件尝试根据集合中的参数定义的类型来强制转换参数。Otherwise, the ObjectDataSource control attempts to cast the parameters according to the type that is defined by the parameters in the collection. 有关详细信息, 请参阅将参数用于 ObjectDataSource 控件For more information, see Using Parameters with the ObjectDataSource Control.

ObjectDataSourceView属性检索与ObjectDataSource控件关联的包含的属性。InsertParameters InsertParametersThe InsertParameters property retrieves the InsertParameters property that is contained by the ObjectDataSourceView that is associated with the ObjectDataSource control.

有关参数合并、对象生存期和方法解析的详细信息, 请参阅InsertMethodFor more information about parameter merging, object lifetime, and method resolution, see InsertMethod.

适用于

另请参阅