Uredi

Deli z drugimi prek


ObjectDataSource.InsertParameters Property

Definition

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

Property Value

A ParameterCollection that contains the parameters used by the method identified by the InsertMethod property.

Attributes

Examples

This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource object with a business object and a DetailsView control to insert data. The second code example provides an example implementation of the Insert method that is used in the first code example.

The following code example demonstrates how to use an ObjectDataSource control with a business object and a DetailsView control to insert data. Initially, the DetailsView displays text boxes in which you can enter data for a new NorthwindEmployee record, along with an automatically generated Insert button. After you enter data into the fields of the DetailsView control, click the Insert button. The InsertMethod property identifies which method performs the insert operation.

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. In this code example, one parameter is specified in the InsertParameters collection that corresponds to the supervisor's ID. 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. 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.

When the Insert operation is performed, the method that is identified by the InsertMethod property is called. If 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.

Important

You should validate any parameter value that you receive from the client. 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>

The following code example provides an example implementation of the Insert method that the preceding code example uses. 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.

To run the example, you must have the NorthwindEmployee class that is provided in the ObjectDataSource class overview. 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. 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

Remarks

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. 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. 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. Otherwise, the ObjectDataSource control attempts to cast the parameters according to the type that is defined by the parameters in the collection. For more information, see Using Parameters with the ObjectDataSource Control.

The InsertParameters property retrieves the InsertParameters property that is contained by the ObjectDataSourceView that is associated with the ObjectDataSource control.

For more information about parameter merging, object lifetime, and method resolution, see InsertMethod.

Applies to

See also