语言

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 包含由属性标识 InsertMethod 的方法使用的参数。

属性

示例

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

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

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

Insert执行操作时,将调用由属性标识InsertMethod的方法。 Insert如果对象的方法具有包含参数的方法签名,则InsertParameters集合必须包含与方法签名参数Insert匹配的参数才能成功完成。

Important

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

该InsertParameters属性检索InsertParameters与ObjectDataSourceView控件关联的属性ObjectDataSource。

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

适用于

另请参阅