ObjectDataSourceObjectEventHandler 代理人

定義

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

C#
public delegate void ObjectDataSourceObjectEventHandler(object sender, ObjectDataSourceEventArgs e);

參數

sender
Object

事件的來源。

範例

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

ASP.NET (C#)
<%@ 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>

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

C#
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;
    }
  }
}

備註

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

擴充方法

GetMethodInfo(Delegate)

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

適用於

產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱