ObjectDataSourceObjectEventHandler 委托
定义
表示将用于处理 ObjectCreating 控件的 ObjectCreated 和 ObjectDataSource 事件的方法。Represents the method that will handle the ObjectCreating and ObjectCreated events of the ObjectDataSource control.
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
事件源。The source of the event.
包含事件数据的 ObjectDataSourceEventArgs。An ObjectDataSourceEventArgs that contains the event data.
示例
下面的代码示例演示如何将 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. 在此示例中,与在许多真实情况下一样,可能无法或不适合将业务对象的默认实例用于 ObjectDataSource 控件。In this example, as in many real-world scenarios, it might not be possible or 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 instantiate 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 used in the preceding example.
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 委托时,需要标识将要处理该事件的方法。When you create an ObjectDataSourceObjectEventHandler delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关如何处理事件的详细信息,请参阅 处理和引发事件。For more information about how to handle events, see Handling and Raising Events.
扩展方法
| GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate. |