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

属性

示例

本部分包含两个代码示例。 第一个代码示例演示如何对业务对象和DetailsView控件使用ObjectDataSource对象来插入数据。 第二个代码示例提供了第一个代码示例中使用的方法的示例实现 Insert

下面的代码示例演示如何对业务对象和DetailsView控件使用ObjectDataSource控件来插入数据。 最初,显示 DetailsView 文本框,你可以在其中输入新 NorthwindEmployee 记录的数据,以及自动生成的 “插入 ”按钮。 在控件的字段中 DetailsView 输入数据后,单击“ 插入 ”按钮。 该 InsertMethod 属性标识执行插入操作的方法。

如果单击 “插入 ”按钮,将使用属性指定的 InsertMethod 方法和集合中指定的 InsertParameters 任何参数来执行该操作。 在此代码示例中,集合中 InsertParameters 指定了一个与监督器 ID 对应的参数。 这是因为,即使该 ID 作为对象显示在控件的Rows集合DetailsView中,它也会作为字符串ObjectDataSource传递给控件。BoundField 通过将它显式添加到集合中 InsertParameters ,将 Type 属性设置为 Int32 值,它将正确传递给 ObjectDataSource 方法作为一个 Int32,而不是字符串。

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 提供的中间层对象中,使对象能够更轻松地在 Web 方案中使用 ObjectDataSource 控件,而无需对实际业务逻辑进行实质性重写。

若要运行该示例,必须具有 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 参数的名称和类型匹配。 参数名称区分大小写。 使用提供参数(如 GridViewDetailsView 控件)的数据绑定控件时,该 ObjectDataSource 控件会自动将集合中显式指定的任何参数与数据绑定控件提供的参数合并。 这一点很重要,因为数据绑定控件始终以类型的形式 String 提供其参数,并且如果方法签名包含数值或日期类型,则必须在具有正确类型的集合中 InsertParameters 显式包含参数。 否则,控件 ObjectDataSource 会尝试根据集合中的参数定义的类型强制转换参数。 有关详细信息,请参阅 将参数与 ObjectDataSource 控件配合使用

InsertParameters属性检索InsertParametersObjectDataSource控件关联的属性ObjectDataSourceView

有关参数合并、对象生存期和方法解析的详细信息,请参阅 InsertMethod

适用于

另请参阅