ObjectDataSourceView.InsertParameters 属性
定义
获取参数集合,该集合包含由 InsertMethod 方法使用的参数。Gets the parameters collection that contains the parameters that are used by the InsertMethod method.
public:
property System::Web::UI::WebControls::ParameterCollection ^ InsertParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
public System.Web.UI.WebControls.ParameterCollection InsertParameters { get; }
member this.InsertParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property InsertParameters As ParameterCollection
属性值
ParameterCollection,它包含 InsertMethod 属性所使用的参数。A ParameterCollection that contains the parameters used by the InsertMethod property.
示例
本部分包含两个代码示例。This section contains two code examples. 第一个代码示例演示如何使用 ObjectDataSource 具有业务对象的控件和 DetailsView 用于插入数据的控件来显示筛选后的数据。The first code example demonstrates how to display filtered data using an ObjectDataSource control 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 记录以及自动生成的 "插入" 按钮。The DetailsView initially displays 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属性标识执行操作的方法 Insert 。The InsertMethod property identifies which method performs the Insert operation.
如果单击 " 插入 " 按钮,则将 Insert 使用由属性指定的方法 InsertMethod 和集合中指定的任何参数执行该操作 InsertParameters 。If you click the Insert button, the Insert operation is performed using the method that is specified by the InsertMethod property and any parameters that are specified in the InsertParameters collection. 在此代码示例中,在 InsertParameters 与主管 ID 相对应的集合中指定一个参数。In this code example, one parameter is specified in the InsertParameters collection that corresponds to the supervisor's ID. 这是因为即使 ID 在控件的集合中显示 Fields DetailsView 为 BoundField 对象,它也将作为字符串传递到 ObjectDataSource 控件。This is because even though the ID is displayed in the Fields collection for the DetailsView control as a BoundField object, it will be passed as a string to the ObjectDataSource control. 通过将其显式添加到 InsertParameters 具有 Type 设置为值的属性的集合,会将 Int32 其 ObjectDataSource 作为 int (而非)的方法正确传递给方法 string 。By adding it explicitly to the InsertParameters collection with a Type property that is set to the Int32 value, it will be passed correctly by the ObjectDataSource to the method as an int, not as string.
Insert执行操作时,将调用由属性标识的方法 InsertMethod 。When the Insert operation is performed, the method that is identified by the InsertMethod property is called. 如果 Insert 对象的方法具有包含参数的方法签名,则 InsertParameters 集合必须包含名称与方法签名参数匹配的参数, Insert 才能成功完成。If the Insert method of the object has a method signature that includes parameters, the InsertParameters collection must contain a parameter with names that match the method signature parameters for the Insert to complete successfully.
<%@ 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. InsertNewEmployeeWrapper方法将添加到 EmployeeLogic 中间层对象,使对象能够更轻松地 ObjectDataSource 在 Web 方案中与控件一起工作,而无需对实际的业务逻辑进行重大重写。The InsertNewEmployeeWrapper method is added to the EmployeeLogic middle-tier object to enable the object to work more easily with the ObjectDataSource control in Web scenarios, without a substantial rewrite to the actual business logic.
// 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 method specified by the InsertMethod property signature. 在使用提供参数的数据绑定控件(如 GridView 和)时, DetailsView ObjectDataSource 控件自动将集合中显式指定的所有参数与数据绑定控件提供的参数合并。When working with data-bound controls that supply parameters, such as GridView and DetailsView, the ObjectDataSource control automatically merges any parameters that are explicitly specified in the collection with those parameters that are provided by the data-bound control. 有关详细信息,请参阅 InsertMethod。For more information, see InsertMethod.