ObjectDataSource.ObjectCreating 事件
2016-07-18 13:56
302 查看
ObjectCreating 事件在创建由 TypeName 属性标识的对象之前发生。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
语法
备注
如果被标识以执行数据操作的方法为 static(Visual Basic 中为 Shared),则永远不会引发 ObjectCreating 和 ObjectCreated 事件。
ObjectDataSource 控件自动调用业务对象的默认构造函数,以使用反射创建该对象的实例。处理 ObjectCreating 事件,
显式调用另一个构造函数,并将结果对象的实例设置为关联 ObjectDataSourceEventArgs 对象的 ObjectInstance 属性。
示例
本节包含两个代码示例。第一个代码示例演示如何将 ObjectDataSource 对象与业务对象和 GridView 控件一起使用来显示信息。
第二个代码示例提供在第一个代码示例中使用的中间层业务对象。
下面的代码示例演示如何将 ObjectDataSource 控件与业务对象和 GridView 控件配合使用来显示信息。
对于网页执行的每个数据操作,您使用的可能是在时间和资源方面需要很大开销才能创建的业务对象。
使用高开销对象的一种方法是:创建一次对象的实例,然后将其缓存起来用于后续操作,
而不是在每次数据操作时就要创建一次对象的实例然后再销毁。
注意
在生产应用程序中,多个请求最后可能会同时使用同一实例。因此,对象需要在线程安全方式中实现。
此代码示例演示了这种模式。您可以处理 ObjectCreating 事件,首先在缓存中检查对象,
然后,仅当尚未缓存对象的实例时才创建实例。然后处理 ObjectDisposing 事件,缓存业务对象供将来使用,
而不是将其销毁。在此代码示例中,将 ObjectDataSourceDisposingEventArgs 对象的 CancelEventArgs.Cancel 属性设置为 true,
以指示 ObjectDataSource 不要调用对象的 Dispose 方法。
下面的代码示例提供了前一代码示例所使用的中间层业务对象的示例。此代码示例由 EmployeeLogic 类定义的基本业务对象组成,
该类是封装业务逻辑的有状态类。要获得完整的可运行示例,必须将此代码编译为库,然后从 ASP.NET 页(.aspx 文件)中使用这些类。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
语法
// 摘要: // 在创建由 System.Web.UI.WebControls.ObjectDataSource.TypeName 属性标识的对象之前发生。 [WebCategory("Data")] [WebSysDescription("ObjectDataSource_ObjectCreating")] public event ObjectDataSourceObjectEventHandler ObjectCreating;
备注
如果被标识以执行数据操作的方法为 static(Visual Basic 中为 Shared),则永远不会引发 ObjectCreating 和 ObjectCreated 事件。
ObjectDataSource 控件自动调用业务对象的默认构造函数,以使用反射创建该对象的实例。处理 ObjectCreating 事件,
显式调用另一个构造函数,并将结果对象的实例设置为关联 ObjectDataSourceEventArgs 对象的 ObjectInstance 属性。
示例
本节包含两个代码示例。第一个代码示例演示如何将 ObjectDataSource 对象与业务对象和 GridView 控件一起使用来显示信息。
第二个代码示例提供在第一个代码示例中使用的中间层业务对象。
下面的代码示例演示如何将 ObjectDataSource 控件与业务对象和 GridView 控件配合使用来显示信息。
对于网页执行的每个数据操作,您使用的可能是在时间和资源方面需要很大开销才能创建的业务对象。
使用高开销对象的一种方法是:创建一次对象的实例,然后将其缓存起来用于后续操作,
而不是在每次数据操作时就要创建一次对象的实例然后再销毁。
注意
在生产应用程序中,多个请求最后可能会同时使用同一实例。因此,对象需要在线程安全方式中实现。
此代码示例演示了这种模式。您可以处理 ObjectCreating 事件,首先在缓存中检查对象,
然后,仅当尚未缓存对象的实例时才创建实例。然后处理 ObjectDisposing 事件,缓存业务对象供将来使用,
而不是将其销毁。在此代码示例中,将 ObjectDataSourceDisposingEventArgs 对象的 CancelEventArgs.Cancel 属性设置为 true,
以指示 ObjectDataSource 不要调用对象的 Dispose 方法。
<%@ Import namespace="Samples.AspNet.CS" %> <%@ Page language="c#" %> <Script runat="server"> // Instead of creating and destroying the business object each time, the // business object is cached in the ASP.NET Cache. private void GetEmployeeLogic(object sender, ObjectDataSourceEventArgs e) { // First check to see if an instance of this object already exists in the Cache. EmployeeLogic cachedLogic; cachedLogic = Cache["ExpensiveEmployeeLogicObject"] as EmployeeLogic; if (null == cachedLogic) { cachedLogic = new EmployeeLogic(); } e.ObjectInstance = cachedLogic; } private void ReturnEmployeeLogic(object sender, ObjectDataSourceDisposingEventArgs e) { // Get the instance of the business object that the ObjectDataSource is working with. EmployeeLogic cachedLogic = e.ObjectInstance as EmployeeLogic; // Test to determine whether the object already exists in the cache. EmployeeLogic temp = Cache["ExpensiveEmployeeLogicObject"] as EmployeeLogic; if (null == temp) { // If it does not yet exist in the Cache, add it. Cache.Insert("ExpensiveEmployeeLogicObject", cachedLogic); } // Cancel the event, so that the object will // not be Disposed if it implements IDisposable. e.Cancel = true; } </Script> <html> <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="GetCreateTime" typename="Samples.AspNet.CS.EmployeeLogic" onobjectcreating="GetEmployeeLogic" onobjectdisposing="ReturnEmployeeLogic" > </asp:objectdatasource> </form> </body> </html>
下面的代码示例提供了前一代码示例所使用的中间层业务对象的示例。此代码示例由 EmployeeLogic 类定义的基本业务对象组成,
该类是封装业务逻辑的有状态类。要获得完整的可运行示例,必须将此代码编译为库,然后从 ASP.NET 页(.aspx 文件)中使用这些类。
namespace Samples.AspNet.CS { using System; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; // // EmployeeLogic is a stateless business object that encapsulates // the operations you can perform on a NorthwindEmployee object. // public class EmployeeLogic { public EmployeeLogic () : this(DateTime.Now) { } public EmployeeLogic (DateTime creationTime) { _creationTime = creationTime; } private DateTime _creationTime; // Returns a collection of NorthwindEmployee objects. public ICollection GetCreateTime () { ArrayList al = new ArrayList(); // Returns creation time for this example. al.Add("The business object that you are using was created at " + _creationTime); return al; } } }
相关文章推荐
- 最新的App上架教程Object-C
- 最新的App上架教程Object-C
- JsonObject 解析 json
- RMI 之 简单的 HelloWorld 实现(三)
- 简谈Masnory的功能和使用
- Objective-C Fast Enumeration 实现原理
- Object::connect函数的学习
- 多线程(3)-基于Object的线程等待与唤醒
- Object类中的toString(),equals(), hashCode()方法之间的关系
- Objective-c中.m、.h、.mm文件
- Xcode中pch文件的使用
- AttributeError: 'module' object has no attribute 'XXX'
- 解决aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: Nosuchfile
- The method list(String, Object[]) is ambiguous for the type BaseHibernateDao<M,PK
- List<Map<String,Object>>如何使用Java代码遍历以获取String,Object的值
- Objective-C中的instancetype和id区别
- Objective-C实现多继承
- 使用json发生java.lang.NoClassDefFoundError: net/sf/json/JSONObject异常的解决办法
- java基础关键字之this 和 super Object 和包装类
- maven+Nexus搭建本地私服仓库