共用方式為


ObjectDataSourceObjectEventHandler 代理人

定義

表示方法,處理 ObjectCreating 控制項的 ObjectCreatedObjectDataSource 事件。

public delegate void ObjectDataSourceObjectEventHandler(System::Object ^ sender, ObjectDataSourceEventArgs ^ e);
public delegate void ObjectDataSourceObjectEventHandler(object sender, ObjectDataSourceEventArgs e);
type ObjectDataSourceObjectEventHandler = delegate of obj * ObjectDataSourceEventArgs -> unit
Public Delegate Sub ObjectDataSourceObjectEventHandler(sender As Object, e As ObjectDataSourceEventArgs)

參數

sender
Object

事件的來源。

範例

下列程式碼範例示範如何使用 ObjectDataSource 控制項搭配商務物件和 GridView 控制項來擷取和顯示資訊。 在此範例中,如同許多真實世界案例,可能無法或適合搭配控制項使用商務物件 ObjectDataSource 的預設實例。 在此範例中 ObjectDataSource ,無法成功呼叫無參數建構函式,因為它會擲回例外狀況。 在某些情況下,無參數建構函式可能會受到保護,而其他建構函式可能無法將商務物件初始化為所需的狀態。 不論原因為何,您可以自行具現化商務物件,並將 實例 ObjectInstance 設定為傳遞至處理常式之 物件的 屬性 ObjectDataSourceEventArgs 。 這是 將用來執行其工作的商務物件實例 ObjectDataSource

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

下列程式碼範例示範上述範例中使用的範例基本商務物件。

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

備註

當您建立 ObjectDataSourceObjectEventHandler 委派 (Delegate) 時,就可以識別即將處理此事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需如何處理事件的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱