• 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;
        }
      }
    }
  • 相关阅读:
    Wonderful hyperlinks of MVVM(ModelViewViewModel)Design Pattern Virus
    [Translation]Silverlight 4MVVM with Commanding and WCF RIA Services Virus
    微软企业库4.1学习笔记(一)开篇乱弹 Virus
    根据总用量计算每种包装规格的购买量和总价 Virus
    微软企业库4.1学习笔记(二)各功能之间的依赖关系以及对象创建 Virus
    silverlight+wcf:relation entity VS. relation entity's code Virus
    根据总用量计算每种包装规格的购买量和总价 后续篇(一)并且使得用户花费最少 Virus
    Team Build Workflow 资源汇总
    VSTF Build Workflow(3)Hello World!
    初探798
  • 原文地址:https://www.cnblogs.com/rinack/p/5680961.html
Copyright © 2020-2023  润新知