Share via


ObjectDataSourceView.InsertParameters Propriété

Définition

Obtient la collection de paramètres qui contient les paramètres utilisés par la méthode InsertMethod.

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

Valeur de propriété

ParameterCollection

ParameterCollection qui contient les paramètres utilisés par la propriété InsertMethod.

Exemples

Cette section contient deux exemples de code. Le premier exemple de code montre comment afficher des données filtrées à l’aide d’un ObjectDataSource contrôle avec un objet métier et un DetailsView contrôle pour insérer des données. Le deuxième exemple de code fournit un exemple d’implémentation de la Insert méthode utilisée dans le premier exemple de code.

L’exemple de code suivant montre comment utiliser un ObjectDataSource contrôle avec un objet métier et un DetailsView contrôle pour insérer des données. Le DetailsView premier enregistrement affiche un nouvel NorthwindEmployee enregistrement, ainsi qu’un bouton Insertion généré automatiquement. Après avoir entré des données dans les champs du DetailsView contrôle, cliquez sur le bouton Insérer . La InsertMethod propriété identifie la méthode qui effectue l’opération Insert .

Si vous cliquez sur le bouton Insérer , l’opération Insert est effectuée à l’aide de la méthode spécifiée par la InsertMethod propriété et de tous les paramètres spécifiés dans la InsertParameters collection. Dans cet exemple de code, un paramètre est spécifié dans la InsertParameters collection qui correspond à l’ID du superviseur. Cela est dû au fait que même si l’ID est affiché dans la Fields collection pour le DetailsView contrôle en tant qu’objet BoundField , il est transmis en tant que chaîne au ObjectDataSource contrôle. En l’ajoutant explicitement à la InsertParameters collection avec une Type propriété définie sur la Int32 valeur, elle sera passée correctement par la ObjectDataSource méthode en tant que int, et non comme string.

Lorsque l’opération Insert est effectuée, la méthode identifiée par la InsertMethod propriété est appelée. Si la Insert méthode de l’objet a une signature de méthode qui inclut des paramètres, la InsertParameters collection doit contenir un paramètre avec des noms qui correspondent aux paramètres de signature de méthode pour que l’opération Insert se termine correctement.

<%@ 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>

L’exemple de code suivant fournit un exemple d’implémentation de la Insert méthode utilisée par l’exemple de code précédent. La InsertNewEmployeeWrapper méthode est ajoutée à l’objet EmployeeLogic de niveau intermédiaire pour permettre à l’objet de fonctionner plus facilement avec le contrôle dans les ObjectDataSource scénarios Web, sans réécriture substantielle dans la logique métier réelle.

// 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

Remarques

Les noms et les types des paramètres contenus dans la InsertParameters collection doivent correspondre aux noms et aux types des paramètres qui se trouvent dans la méthode spécifiée par la InsertMethod signature de propriété. Lorsque vous utilisez des contrôles liés aux données qui fournissent des paramètres, tels que GridView et DetailsView, le ObjectDataSource contrôle fusionne automatiquement tous les paramètres spécifiés explicitement dans la collection avec ces paramètres fournis par le contrôle lié aux données. Pour plus d'informations, consultez InsertMethod.

S’applique à

Voir aussi