ObjectDataSourceEventArgs.ObjectInstance 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定物件,表示 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