ObjectDataSourceEventArgs.ObjectInstance 属性

定义

获取或设置表示一个业务对象的对象,ObjectDataSource 控件使用该业务对象执行数据操作。

public:
 property System::Object ^ ObjectInstance { System::Object ^ get(); void set(System::Object ^ value); };
public object ObjectInstance { get; set; }
member this.ObjectInstance : obj with get, set
Public Property ObjectInstance As Object

属性值

ObjectDataSource 用来执行数据操作的业务对象;如果将 null 传递给 ObjectDataSourceEventArgs,则为 null

示例

本部分包含两个代码示例。 第一个 ObjectDataSource 代码示例演示如何将控件与业务对象和 GridView 控件配合使用来检索和显示信息。 第二个代码示例提供第一个代码示例使用的示例基本业务对象。

下面的代码示例演示如何将 ObjectDataSource 控件与业务对象和 GridView 控件一起使用来检索和显示信息。 在此示例中,与许多实际方案一样,可能无法也不适合将业务对象的默认实例与 控件一起使用 ObjectDataSource 。 在此示例中, ObjectDataSource 无法成功调用无参数构造函数,因为它将引发异常。 在某些情况下,无参数构造函数可能会受到保护,而在某些情况下,它可能不会将业务对象初始化为所需状态。 无论出于什么原因,都可以自行创建业务对象的实例,并将该实例设置为 ObjectInstance 传递给处理程序的 ObjectDataSourceEventArgs 对象的 属性。 这是 将用于执行其工作的业务对象实例 ObjectDataSource

<%@ 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>

下面的代码示例演示前面的代码示例使用的示例基本业务对象。

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

适用于

另请参阅