Bagikan melalui


ObjectDataSourceView.InsertParameters Properti

Definisi

Mendapatkan koleksi parameter yang berisi parameter yang digunakan oleh InsertMethod metode .

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

Nilai Properti

ParameterCollection yang berisi parameter yang digunakan oleh InsertMethod properti .

Contoh

Bagian ini berisi dua contoh kode. Contoh kode pertama menunjukkan cara menampilkan data yang difilter menggunakan ObjectDataSource kontrol dengan objek bisnis dan DetailsView kontrol untuk menyisipkan data. Contoh kode kedua menyediakan contoh implementasi Insert metode yang digunakan dalam contoh kode pertama.

Contoh kode berikut menunjukkan cara menggunakan ObjectDataSource kontrol dengan objek bisnis dan DetailsView kontrol untuk menyisipkan data. Awalnya DetailsView menampilkan rekaman baru NorthwindEmployee , bersama dengan tombol Sisipkan yang dihasilkan secara otomatis. Setelah Anda memasukkan data ke dalam bidang DetailsView kontrol, klik tombol Sisipkan . Properti InsertMethod mengidentifikasi metode mana yang Insert melakukan operasi.

Jika Anda mengklik tombol Sisipkan , Insert operasi dilakukan menggunakan metode yang ditentukan oleh InsertMethod properti dan parameter apa pun yang ditentukan dalam InsertParameters koleksi. Dalam contoh kode ini, satu parameter ditentukan dalam InsertParameters koleksi yang sesuai dengan ID penyelia. Ini karena meskipun ID ditampilkan dalam Fields koleksi untuk DetailsView kontrol sebagai BoundField objek, ID akan diteruskan sebagai string ke ObjectDataSource kontrol. Dengan menambahkannya secara eksplisit ke InsertParameters koleksi dengan Type properti yang diatur ke Int32 nilai , itu akan diteruskan dengan benar oleh ObjectDataSource ke metode sebagai int, bukan sebagai string.

Insert Ketika operasi dilakukan, metode yang diidentifikasi oleh InsertMethod properti dipanggil. Insert Jika metode objek memiliki tanda tangan metode yang menyertakan parameter, InsertParameters koleksi harus berisi parameter dengan nama yang cocok dengan parameter tanda tangan metode agar Insert berhasil diselesaikan.

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

Contoh kode berikut memberikan contoh implementasi Insert metode yang digunakan contoh kode sebelumnya. Metode InsertNewEmployeeWrapper ini ditambahkan ke EmployeeLogic objek tingkat menengah untuk memungkinkan objek bekerja lebih mudah dengan ObjectDataSource kontrol dalam skenario Web, tanpa penulisan ulang yang substansial ke logika bisnis yang sebenarnya.

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

Keterangan

Nama dan jenis parameter yang terkandung dalam InsertParameters koleksi harus cocok dengan nama dan jenis parameter yang ada dalam metode yang ditentukan oleh InsertMethod tanda tangan properti. Saat bekerja dengan kontrol terikat data yang menyediakan parameter, seperti GridView dan DetailsView, ObjectDataSource kontrol secara otomatis menggabungkan parameter apa pun yang secara eksplisit ditentukan dalam koleksi dengan parameter yang disediakan oleh kontrol terikat data. Untuk informasi selengkapnya, lihat InsertMethod .

Berlaku untuk

Lihat juga