ObjectDataSourceView.InsertParameters Tulajdonság

Definíció

Lekéri a metódus által használt paramétereket tartalmazó paramétergyűjteményt 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

Tulajdonság értéke

A ParameterCollection tulajdonság által InsertMethod használt paramétereket tartalmazó.

Példák

Ez a szakasz két példakódot tartalmaz. Az első példakód bemutatja, hogyan jeleníthet meg szűrt adatokat egy ObjectDataSource üzleti objektummal és egy DetailsView vezérlővel az adatok beszúrásához. A második példakód az első kód példában használt metódus implementációját Insert szemlélteti.

Az alábbi példakód bemutatja, hogyan használható egy ObjectDataSource vezérlő egy üzleti objektummal és egy vezérlővel DetailsView az adatok beszúrására. A DetailsView kezdetben megjelenik egy új NorthwindEmployee rekord, valamint egy automatikusan létrehozott Beszúrás gomb. Miután adatokat ad meg a DetailsView vezérlő mezőibe, kattintson a Beszúrás gombra. A InsertMethod tulajdonság azonosítja, hogy melyik metódus hajtja végre a Insert műveletet.

Ha a Beszúrás gombra kattint, a Insert művelet a tulajdonság által megadott módszerrel és a InsertMethodInsertParameters gyűjteményben megadott paraméterekkel történik. Ebben a kód példában egy paraméter van megadva a InsertParameters gyűjteményben, amely megfelel a felügyelő azonosítójának. Ennek az az oka, hogy annak ellenére, hogy az azonosító objektumként Fields jelenik meg a DetailsViewBoundField vezérlő gyűjteményében, a rendszer sztringként továbbítja azt a ObjectDataSource vezérlőnek. Ha explicit módon hozzáadja a InsertParameters gyűjteményhez egy Type olyan tulajdonsággal, amely az Int32 értékre van állítva, a metódus ObjectDataSourcehelyesen int adja át, nem pedig mintstring.

Insert A művelet végrehajtásakor a rendszer meghívja a tulajdonság által InsertMethod azonosított metódust. Ha az Insert objektum metódusa rendelkezik paramétereket tartalmazó metódus-aláírással, a InsertParameters gyűjteménynek tartalmaznia kell egy olyan paramétert, amelynek neve megegyezik a metódus aláírási paramétereivel a Insert sikeres befejezéshez.

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

Az alábbi példakód az előző kód példája által használt metódus implementálását Insert szemlélteti. A InsertNewEmployeeWrapper rendszer hozzáadja a metódust a EmployeeLogic középső rétegbeli objektumhoz, hogy az objektum könnyebben működjön a ObjectDataSource webes forgatókönyvek vezérlőjével anélkül, hogy a tényleges üzleti logikát jelentősen átírták.

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

Megjegyzések

A gyűjteményben InsertParameters található paraméterek nevének és típusának meg kell egyeznie a tulajdonságadék által InsertMethod megadott metódusban megadott paraméterek nevével és típusával. Ha olyan adathoz kötött vezérlőkkel dolgozik, amelyek paramétereket biztosítanak, például GridView és DetailsView, a ObjectDataSource vezérlő automatikusan egyesíti a gyűjteményben explicit módon megadott paramétereket az adatkötött vezérlő által biztosított paraméterekkel. További információért lásd InsertMethod.

A következőre érvényes:

Lásd még