ObjectDataSourceObjectEventHandler 委托

定义

表示将用于处理 ObjectCreating 控件的 ObjectCreatedObjectDataSource 事件的方法。

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

参数

sender
Object

事件源。

示例

下面的代码示例演示如何将控件与业务对象和GridView控件配合使用ObjectDataSource来检索和显示信息。 在此示例中,与许多实际方案一样,可能无法或不适合将业务对象的默认实例与 控件一起使用 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 委托时,需要标识将要处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关如何处理事件的详细信息,请参阅 处理和引发事件

扩展方法

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

另请参阅