ObjectDataSourceEventArgs 類別

定義

提供資料給 ObjectCreating 控制項的 ObjectCreatedObjectDataSource 事件。Provides data for the ObjectCreating and ObjectCreated events of the ObjectDataSource control.

public ref class ObjectDataSourceEventArgs : EventArgs
public class ObjectDataSourceEventArgs : EventArgs
type ObjectDataSourceEventArgs = class
    inherit EventArgs
Public Class ObjectDataSourceEventArgs
Inherits EventArgs
繼承
ObjectDataSourceEventArgs

範例

本節包含兩個程式碼範例。This section contains two code examples. 第一個程式碼範例示範如何將 ObjectDataSource 控制項與商務物件搭配使用,並使用 GridView 控制項來取得和顯示資訊。The first code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to retrieve and display information. 第二個程式碼範例會提供第一個程式碼範例所使用的範例基本商務物件。The second code example provides the example basic business object that the first code example uses.

下列程式碼範例示範如何將 ObjectDataSource 控制項與商務物件和控制項搭配使用, GridView 以取得和顯示資訊。The following code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to retrieve and display information. 在此範例中,如同在許多真實案例中,使用商務物件的預設實例與控制項可能不可能也不適合 ObjectDataSourceIn this example, as in many real-world scenarios, it might not be possible nor appropriate to use a default instance of the business object with the ObjectDataSource control. 在此範例中, ObjectDataSource 無法成功呼叫無參數的函式,因為它會擲回例外狀況。In this example, the ObjectDataSource cannot successfully call the parameterless constructor because it will throw an exception. 在某些情況下,無參數的函式可能會受到保護,而在其他情況下,它可能不會將商務物件初始化為所需的狀態。In some cases, the parameterless constructor might be protected and in others it might not initialize the business object to a desired state. 無論原因為何,您都可以自行建立商務物件的實例,並將實例設為 ObjectInstance ObjectDataSourceEventArgs 傳遞給處理常式之物件的屬性。Whatever the reason, you can create an instance of the business object yourself and set the instance to the ObjectInstance property of the ObjectDataSourceEventArgs object that is passed to the handler. 這是 ObjectDataSource 將用來執行其工作的商務物件實例。This is the business object instance that the ObjectDataSource will use to perform its work.

<%@ 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">
<script runat="server">
private void NorthwindLogicCreating(object sender, ObjectDataSourceEventArgs e)
{
    // Create an instance of the business object using a non-default constructor.
    EmployeeLogic eLogic = new EmployeeLogic("Not created by the default constructor!");
    
    // Set the ObjectInstance property so that the ObjectDataSource uses the created instance.
    e.ObjectInstance = eLogic;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          onobjectcreating="NorthwindLogicCreating"
          typename="Samples.AspNet.CS.EmployeeLogic" >
        </asp:objectdatasource>

    </form>
  </body>
</html>
<%@ 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">
<script runat="server">
Private Sub NorthwindLogicCreating(sender As Object, e As ObjectDataSourceEventArgs)

    ' Create an instance of the business object using a non-default constructor.
    Dim eLogic As EmployeeLogic = New EmployeeLogic("Not created by the default constructor!")
    
    ' Set the ObjectInstance property so that the ObjectDataSource uses the created instance.
    e.ObjectInstance = eLogic
    
End Sub ' NorthwindLogicCreating

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          onobjectcreating="NorthwindLogicCreating"
          typename="Samples.AspNet.VB.EmployeeLogic" >
        </asp:objectdatasource>

    </form>
  </body>
</html>

下列程式碼範例示範上述程式碼範例所使用的範例基本商務物件。The following code example demonstrates the example basic business object that the preceding code example uses.

namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

  public class EmployeeLogic {

    public EmployeeLogic() {  
        throw new NotSupportedException("Initialize data.");
    }
    
    public EmployeeLogic(string data) {
        _data = data;
    }

    private string _data;
    
    // Returns a collection of NorthwindEmployee objects.
    public ICollection GetAllEmployees () {
      ArrayList al = new ArrayList();      
      al.Add(_data);        
      return al;
    }
  }
}
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB
  Public Class EmployeeLogic
    
    
    Public Sub New() 
        Throw New NotSupportedException("Initialize data.")
    
    End Sub
    
    
    Public Sub New(ByVal data As String) 
        _data = data
    
    End Sub
    
    Private _data As String
    
    
    ' Returns a collection of NorthwindEmployee objects.
    Public Function GetAllEmployees() As ICollection 
        Dim al As New ArrayList()
        al.Add(_data)
        Return al
    
    End Function 'GetAllEmployees
  End Class
End Namespace ' Samples.AspNet.VB

備註

ObjectDataSourceEventArgs類別會在 OnObjectCreating 和方法中用 OnObjectCreated 來供應商業物件實例的存取權,然後才會執行任何使用 ObjectDataSource control 和 business 物件的資料作業。The ObjectDataSourceEventArgs class is used in the OnObjectCreating and OnObjectCreated methods to provide access to the business object instance before any data operations that are using the ObjectDataSource control and business object are performed. 商務物件是使用屬性來設定和存取 ObjectInstanceThe business object is set and accessed using the ObjectInstance property. 藉由新增事件處理常式委派來處理 ObjectCreating 事件,您可以在自訂程式碼中建立商務物件的實例,而不是執行具現 ObjectDataSource 化。By adding an event handler delegate to handle the ObjectCreating event, you can create an instance of the business object in custom code instead of the ObjectDataSource performing the instantiation. 當您想要商務物件的非預設實例,或呼叫非無參數的函式來建立實例時,這非常有用: ObjectDataSource 一律會呼叫無參數的函式,以建立其所使用之商務物件的實例。This is useful when you want a non-default instance of your business object or to call a non-parameterless constructor to create the instance; the ObjectDataSource always calls the parameterless constructor to create an instance of the business object it works with. 您也可以新增事件處理常式委派來處理 ObjectCreated 事件,這樣可讓您存取商務物件的任何公開公開成員,以執行任何額外的初始化或工作。You can also add an event handler delegate to handle the ObjectCreated event, which enables you to access any publicly exposed members of the business object to perform any additional initialization or work.

OnObjectCreating OnObjectCreated ObjectDataSource 如果執行資料作業的商務物件方法是,則控制項不會呼叫和方法 staticThe OnObjectCreating and OnObjectCreated methods are not called by the ObjectDataSource control, if the business object method that performs the data operations is static.

ObjectDataSource控制項會公開許多事件,讓您可以處理這些事件,以在其生命週期的不同時間處理基礎商務物件。The ObjectDataSource control exposes many events that you can handle to work with the underlying business object at various times in its lifecycle. 下表列出事件以及相關聯的 EventArgs 類別和事件處理常式委派。The following table lists the events and the associated EventArgs classes and event handler delegates.

事件Event EventArgsEventArgs EventHandlerEventHandler
ObjectCreating.ObjectCreating.

在建立商務物件的實例之前立即發生。Occurs immediately before the instance of the business object is created.
ObjectDataSourceEventArgs ObjectDataSourceObjectEventHandler
ObjectCreated.ObjectCreated.

在建立商務物件的實例之後立即發生。Occurs immediately after the instance of the business object is created.
ObjectDataSourceEventArgs ObjectDataSourceObjectEventHandler
Selecting.Selecting.

在資料取出之前發生。Occurs before the data is retrieved.
ObjectDataSourceSelectingEventArgs ObjectDataSourceSelectingEventHandler
InsertingUpdatingDeletingInserting, Updating, and Deleting.

在執行插入、更新或刪除作業之前發生。Occur before an insert, update, or delete operation is performed.
ObjectDataSourceMethodEventArgs ObjectDataSourceMethodEventHandler
Selected.Selected.

在資料被取出之後發生。Occurs after the data is retrieved.
ObjectDataSourceStatusEventArgs ObjectDataSourceStatusEventHandler
Inserted, Updated, Deleted.Inserted, Updated, Deleted.

在插入、更新或刪除作業完成之後發生。Occur after the insert, update, or delete operation is completed.
ObjectDataSourceStatusEventArgs ObjectDataSourceStatusEventHandler
ObjectDisposing.ObjectDisposing.

在損毀商務物件之前發生。Occurs before a business object is destroyed.
ObjectDataSourceDisposingEventArgs ObjectDataSourceDisposingEventHandler

建構函式

ObjectDataSourceEventArgs(Object)

使用指定的物件,初始化 ObjectDataSourceEventArgs 類別的新執行個體。Initializes a new instance of the ObjectDataSourceEventArgs class using the specified object.

屬性

ObjectInstance

取得或設定物件,表示 ObjectDataSource 用來執行資料作業的商務物件。Gets or sets an object that represents the business object with which the ObjectDataSource control performs data operations.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱