ObjectDataSource.ObjectCreating 事件

ObjectCreating 事件在创建由 TypeName 属性标识的对象之前发生。

命名空间: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;
    }
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值