ObjectDataSource 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示向多層 Web 應用程式架構中資料繫結控制項提供資料的商務物件 (Business Object)。
public ref class ObjectDataSource : System::Web::UI::DataSourceControl
[System.Drawing.ToolboxBitmap(typeof(System.Web.UI.WebControls.ObjectDataSource))]
public class ObjectDataSource : System.Web.UI.DataSourceControl
[<System.Drawing.ToolboxBitmap(typeof(System.Web.UI.WebControls.ObjectDataSource))>]
type ObjectDataSource = class
inherit DataSourceControl
Public Class ObjectDataSource
Inherits DataSourceControl
- 繼承
- 屬性
範例
本節會顯示 ObjectDataSource .aspx 頁面中的 標記,並顯示其使用的商務物件。 此範例為 .aspx 頁面。 它包含 GridView 系結至控制項的 ObjectDataSource 控制項。 ObjectDataSource控制項標記會指定要呼叫以擷取資料之商務物件名稱和商務物件方法的名稱。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="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">
<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:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
typename="Samples.AspNet.CS.EmployeeLogic" />
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - Visual Basic Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1" />
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
typename="Samples.AspNet.VB.EmployeeLogic" />
</form>
</body>
</html>
下列範例顯示 .aspx 頁面中控制項所使用的商務物件 ObjectDataSource 。 (許多其他 ObjectDataSource 程式碼範例也會使用此商務物件。) 此範例包含下列兩個基本類別:
類別
EmployeeLogic
是 所使用的商務邏輯類別 ObjectDataSource 。類別
NorthwindEmployee
會定義 類別的 方法EmployeeLogic
所傳GetAllEmployees
回的資料物件。
為了方便起見,會提供額外的 NorthwindDataException
類別。
這組範例類別適用于 Northwind Traders 資料庫,此資料庫適用于 Microsoft SQL Server 和 Microsoft Access。 如需完整的工作範例,您必須編譯並使用這些類別搭配提供的 .aspx 頁面範例。
namespace Samples.AspNet.CS {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations one can perform on a NorthwindEmployee object.
//
public class EmployeeLogic {
// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees () {
ArrayList al = new ArrayList();
ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];
SqlDataSource sds
= new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees");
try {
IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty);
// Iterate through the Enumeration and create a
// NorthwindEmployee object for each ID.
foreach (DataRowView row in IDs) {
string id = row["EmployeeID"].ToString();
NorthwindEmployee nwe = new NorthwindEmployee(id);
// Add the NorthwindEmployee object to the collection.
al.Add(nwe);
}
}
finally {
// If anything strange happens, clean up.
sds.Dispose();
}
return al;
}
public static NorthwindEmployee GetEmployee(object anID) {
return new NorthwindEmployee(anID);
}
public static void UpdateEmployeeInfo(NorthwindEmployee ne) {
bool retval = ne.Save();
if (! retval) { throw new NorthwindDataException("UpdateEmployee failed."); }
}
public static void DeleteEmployee(NorthwindEmployee ne) { }
}
public class NorthwindEmployee {
public NorthwindEmployee () {
ID = DBNull.Value;
lastName = "";
firstName = "";
title="";
titleOfCourtesy = "";
reportsTo = -1;
}
public NorthwindEmployee (object anID) {
this.ID = anID;
ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];
SqlConnection conn = new SqlConnection (cts.ConnectionString);
SqlCommand sc =
new SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " +
" FROM Employees " +
" WHERE EmployeeID = @empId",
conn);
// Add the employee ID parameter and set its value.
sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value = Int32.Parse(anID.ToString());
SqlDataReader sdr = null;
try {
conn.Open();
sdr = sc.ExecuteReader();
// This is not a while loop. It only loops once.
if (sdr != null && sdr.Read()) {
// The IEnumerable contains DataRowView objects.
this.firstName = sdr["FirstName"].ToString();
this.lastName = sdr["LastName"].ToString();
this.title = sdr["Title"].ToString();
this.titleOfCourtesy = sdr["TitleOfCourtesy"].ToString();
if (! sdr.IsDBNull(4)) {
this.reportsTo = sdr.GetInt32(4);
}
}
else {
throw new NorthwindDataException("Data not loaded for employee id.");
}
}
finally {
try {
if (sdr != null) sdr.Close();
conn.Close();
}
catch (SqlException) {
// Log an event in the Application Event Log.
throw;
}
}
}
private object ID;
private string lastName;
public string LastName {
get { return lastName; }
set { lastName = value; }
}
private string firstName;
public string FirstName {
get { return firstName; }
set { firstName = value; }
}
private string title;
public String Title {
get { return title; }
set { title = value; }
}
private string titleOfCourtesy;
public string Courtesy {
get { return titleOfCourtesy; }
set { titleOfCourtesy = value; }
}
private int reportsTo;
public int Supervisor {
get { return reportsTo; }
set { reportsTo = value; }
}
public bool Save () {
return true;
}
}
internal class NorthwindDataException: Exception {
public NorthwindDataException(string msg) : base (msg) { }
}
}
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB
'
' EmployeeLogic is a stateless business object that encapsulates
' the operations you can perform on a NorthwindEmployee object.
' When the class is written in Visual Basic, you cannot use the Shared
' part.
Public Class EmployeeLogic
' Returns a collection of NorthwindEmployee objects.
Public Shared Function GetAllEmployees() As ICollection
Dim al As New ArrayList()
Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees")
Try
Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)
' Iterate through the Enumeration and create a
' NorthwindEmployee object for each ID.
For Each row As DataRowView In IDs
Dim id As String = row("EmployeeID").ToString()
Dim nwe As New NorthwindEmployee(id)
' Add the NorthwindEmployee object to the collection.
al.Add(nwe)
Next
Finally
' If anything strange happens, clean up.
sds.Dispose()
End Try
Return al
End Function 'GetAllEmployees
Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee
Return New NorthwindEmployee(anID)
End Function 'GetEmployee
Public Shared Sub UpdateEmployeeInfo(ne As NorthwindEmployee)
Dim retval As Boolean = ne.Save()
If Not retval Then
Throw New NorthwindDataException("UpdateEmployee failed.")
End If
End Sub
Public Shared Sub DeleteEmployee(ne As NorthwindEmployee)
End Sub
End Class
Public Class NorthwindEmployee
Public Sub New()
ID = DBNull.Value
aLastName = ""
aFirstName = ""
aTitle = ""
titleOfCourtesy = ""
reportsTo = - 1
End Sub
Public Sub New(anID As Object)
Me.ID = anID
Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
Dim conn As New SqlConnection(cts.ConnectionString)
Dim sc As New SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " & _
" FROM Employees " & _
" WHERE EmployeeID = @empId", conn)
' Add the employee ID parameter and set its value.
sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString())
Dim sdr As SqlDataReader = Nothing
Try
conn.Open()
sdr = sc.ExecuteReader()
' This is not a while loop. It only loops once.
If Not (sdr Is Nothing) AndAlso sdr.Read() Then
' The IEnumerable contains DataRowView objects.
Me.aFirstName = sdr("FirstName").ToString()
Me.aLastName = sdr("LastName").ToString()
Me.aTitle = sdr("Title").ToString()
Me.titleOfCourtesy = sdr("TitleOfCourtesy").ToString()
If Not sdr.IsDBNull(4) Then
Me.reportsTo = sdr.GetInt32(4)
End If
Else
Throw New NorthwindDataException("Data not loaded for employee id.")
End If
Finally
Try
If Not (sdr Is Nothing) Then
sdr.Close()
End If
conn.Close()
Catch se As SqlException
' Log an event in the Application Event Log.
Throw
End Try
End Try
End Sub
Private ID As Object
Private aLastName As String
Public Property LastName() As String
Get
Return aLastName
End Get
Set
aLastName = value
End Set
End Property
Private aFirstName As String
Public Property FirstName() As String
Get
Return aFirstName
End Get
Set
aFirstName = value
End Set
End Property
Private aTitle As String
Public Property Title() As String
Get
Return aTitle
End Get
Set
aTitle = value
End Set
End Property
Private titleOfCourtesy As String
Public Property Courtesy() As String
Get
Return titleOfCourtesy
End Get
Set
titleOfCourtesy = value
End Set
End Property
Private reportsTo As Integer
Public Property Supervisor() As Integer
Get
Return reportsTo
End Get
Set
reportsTo = value
End Set
End Property
Public Function Save() As Boolean
Return True
End Function 'Save
End Class
Friend Class NorthwindDataException
Inherits Exception
Public Sub New(msg As String)
MyBase.New(msg)
End Sub
End Class
End Namespace
備註
本主題內容:
簡介
控制項 ObjectDataSource 適用于您所建立的類別。 您可以建立擷取和更新資料的方法,並將這些方法 ObjectDataSource 的名稱提供給標記中的 控制項。 在轉譯或回傳處理期間,會 ObjectDataSource 呼叫您指定的方法。
控制項沒有視覺化轉 ObjectDataSource 譯。 因此, ObjectDataSource 不支援 或 SkinID 屬性等 EnableTheming 視覺功能。
目的
常見的應用程式設計做法是將呈現層與商務邏輯分開,並將商務邏輯封裝在商務物件中。 這些商務物件會在展示層和資料層之間形成不同的層,因而產生三層式應用程式架構。 控制項 ObjectDataSource 可讓開發人員使用 ASP.NET 資料來源控制項,同時保留其三層式應用程式架構。
控制項 ObjectDataSource 會使用反映來建立商務物件的實例,並在它們上呼叫方法來擷取、更新、插入和刪除資料。 屬性 TypeName 會識別 所使用的類別 ObjectDataSource 名稱。 控制項 ObjectDataSource 會針對每個方法呼叫建立和終結 類別的實例;它不會在 Web 要求的存留期內將物件保留在記憶體中。 如果您使用的商務物件需要許多資源,或建立和終結成本高昂,這是一個嚴重的考慮。 使用昂貴的物件可能不是最佳設計選擇,但您可以使用 、 ObjectCreated 和 ObjectDisposing 事件來控制物件的 ObjectCreating 生命週期。
注意
、、 和 屬性所 SelectMethod 識別的方法可以是 Visual Basic) 方法中的實例方法或 static
(Shared
。 DeleteMethodInsertMethodUpdateMethod 如果在 Visual Basic) 中 (方法 static
,則不會建立商務物件的實例,而且 ObjectCreating 不會引發 、 ObjectCreated 和 ObjectDisposingShared
事件。
擷取資料
若要從商務物件擷取資料,請將 SelectMethod 屬性設定為擷取資料的方法名稱。 如果方法未傳回 IEnumerable 或 DataSet 物件,物件會由集合中的 IEnumerable 執行時間包裝。 如果方法簽章具有參數,您可以將物件新增 Parameter 至 SelectParameters 集合,然後將這些物件系結至您想要傳遞給 屬性所 SelectMethod 指定方法的值。 為了讓 ObjectDataSource 控制項使用參數,參數必須符合方法簽章中參數的名稱和類型。 如需詳細資訊,請參閱 搭配 ObjectDataSource 控制項使用參數。
每當呼叫 方法時, Select 控制項 ObjectDataSource 就會擷取資料。 這個方法可讓您以程式設計方式存取 屬性所 SelectMethod 指定的方法。 屬性所 SelectMethod 指定的方法是由呼叫方法 DataBind
時系結至 的 ObjectDataSource 控制項自動呼叫。 如果您設定 DataSourceID
資料繫結控制項的 屬性,控制項會視需要自動系結至資料來源中的資料。 DataSourceID
設定 屬性是將控制項系 ObjectDataSource 結至資料繫結控制項的建議方法。 或者,您可以設定 DataSource
屬性,但接著您必須明確呼叫 DataBind
資料繫結控制項的 方法。 您可以隨時以程式設計方式呼叫 Select 方法來擷取資料。
如需將資料繫結控制項系結至資料來源控制項的詳細資訊,請參閱 使用資料來源控制項系結至資料。
執行資料作業
視控制項所使用的商務物件 ObjectDataSource 功能而定,您可以執行資料作業,例如更新、插入和刪除。 若要執行這些資料作業,請為您想要執行的作業設定適當的方法名稱和任何相關聯的參數。 例如,針對更新作業,請將 UpdateMethod 屬性設定為執行更新的商務物件方法名稱,並將任何必要的參數新增至 UpdateParameters 集合。 ObjectDataSource如果控制項與資料繫結控制項相關聯,資料繫結控制項就會新增參數。 在此情況下,您必須確保方法的參數名稱符合資料繫結控制項中的功能變數名稱。 當您的程式碼明確呼叫 方法或由資料繫結控制項自動呼叫方法時 Update ,就會執行更新。 和 Insert 作業會 Delete 遵循相同的一般模式。 假設商務物件會一次執行一筆記錄的這類資料作業,而不是批次處理。
篩選資料
如果資料傳回為 DataSet 或 DataTable 物件,控制項 ObjectDataSource 可以篩選 屬性所 SelectMethod 擷取的資料。 您可以使用格式字串語法,將運算式中的值系結至集合中指定的 FilterParameters 參數,將屬性設定 FilterExpression 為篩選運算式。
Caching
ObjectDataSource雖然 不會跨多個要求保留商務物件的實例,但它可以快取呼叫 屬性所 SelectMethod 識別的方法結果。 快取資料時,後續呼叫 Select 方法會傳回快取的資料,而不是建立商務物件,並使用反映來呼叫它 SelectMethod 。 快取可讓您避免建立物件,並呼叫其資料方法,以犧牲 Web 服務器上的記憶體。 當 屬性設定為 true
時 EnableCaching ,會自動 ObjectDataSource 快取資料,而 CacheDuration 屬性會設定為快取在捨棄快取之前儲存資料的秒數。 您也可以指定 CacheExpirationPolicy 屬性和選擇性 SqlCacheDependency 屬性。 控制項 ObjectDataSource 可讓您快取所有類型的資料,但不應該快取保留無法共用給多個 (要求的資源或狀態的物件,例如開啟 SqlDataReader 的物件) ,因為相同的物件實例將用來服務多個要求。
特性
下表描述 控制項的功能 ObjectDataSource 。
功能 | 需求 |
---|---|
選取 | 將 SelectMethod 屬性設定為商務物件方法的名稱,以程式設計方式或使用資料繫結控制項,在集合中包含 SelectParameters 任何必要的參數。 |
排序 | 將 SortParameterName 屬性設定為方法中 SelectMethod 具有排序準則的參數名稱。 |
篩選 | 將 FilterExpression 屬性設定為篩選運算式,並選擇性地將任何參數新增至集合, FilterParameters 以在呼叫 方法時 Select 篩選資料。 屬性所 SelectMethod 指定的方法必須傳 DataSet 回 或 DataTable 。 |
Paging | 如果 SelectMethod 方法包含要擷取的最大記錄數目參數,以及要擷取之第一筆記錄的索引,則支援資料來源分頁。 這些參數的名稱必須分別在 和 StartRowIndexParameterName 屬性中 MaximumRowsParameterName 設定。 即使 ObjectDataSource 控制項不支援直接在 屬性所 SelectMethod 指定的 方法中分頁,資料繫結控制項也可以自行執行分頁。 資料繫結控制項必須能夠執行這項操作,就是 屬性所 SelectMethod 指定的 方法會傳回實作 ICollection 介面的物件。 |
更新 | 將 UpdateMethod 屬性設定為更新資料的商務物件方法名稱,並在集合中包含 UpdateParameters 任何必要參數。 |
刪除 | 將 DeleteMethod 屬性設定為刪除資料的商務物件方法或函式名稱,並在集合中包含 DeleteParameters 任何必要參數。 |
插入 | 將 InsertMethod 屬性設定為插入資料的商務物件方法或函式名稱,並在集合中包含 InsertParameters 任何必要的參數。 |
Caching | 根據快取資料所需的快取行為,將 EnableCaching 屬性設定為 true 和 CacheDurationCacheExpirationPolicy 屬性。 |
注意
當您使用 ObjectDataSource 類別來更新或插入資料時,在用戶端輸入的字串不會從用戶端文化特性格式自動轉換成伺服器文化特性格式。 例如,用戶端文化特性可能會將 DD/MM/YYYY 指定為日期格式,而伺服器上的日期格式可能是 MM/DD/YYYY。 在此情況下,2009 年 10 月 5 日會在控制項中 TextBox 輸入為 5/10/2009,但會解譯為 2009 年 5 月 10 日。 2009 年 10 月 15 日會輸入為 2009/15/10,並拒絕為無效日期。
資料檢視
如同所有資料來源控制項,控制項 ObjectDataSource 會與資料來源檢視類別相關聯。 ObjectDataSource雖然控制項是頁面開發人員用來處理資料的介面,但 ObjectDataSourceView 類別是資料繫結控制項使用的介面。 此外,類別 ObjectDataSourceView 會描述資料來源控制項的功能,並執行實際的工作。 控制項 ObjectDataSource 只有一個相關聯的 ObjectDataSourceView ,而且一律名為 DefaultView
。 ObjectDataSourceView雖然 物件是由 GetView 方法公開,但其許多屬性和方法都會由 ObjectDataSource 控制項直接包裝和公開。 在幕後, ObjectDataSourceView 物件會執行所有資料作業,包括擷取、插入、更新、刪除、篩選及排序資料。 如需詳細資訊,請參閱ObjectDataSourceView。
使用 LINQ to SQL
您可以使用 ObjectDataSource 控制項搭配 LINQ to SQL 類別。 若要這樣做,請將 屬性設定 TypeName 為數據內容類別別的名稱。 您也可以將 SelectMethod 、 UpdateMethod 和 InsertMethodDeleteMethod 方法設定為數據內容類別別中執行對應作業的方法。 您必須為 ObjectDisposing 事件建立事件處理常式,才能取消處置資料內容類別別。 此步驟是必要的,因為LINQ to SQL支援延後執行,而 ObjectDataSource 控制項則會嘗試在選取作業之後處置資料內容。 如需如何建立LINQ to SQL類別的詳細資訊,請參閱如何:在 Web 專案中建立LINQ to SQL類別。 如需如何取消處置資料內容類別別的範例,請參閱 ObjectDisposing 事件。
使用 Entity Framework
您也可以搭配 ObjectDataSource Entity Framework 使用 控制項。 如需詳細資訊,請參閱 使用 Entity Framework 和 ObjectDataSource 控制項。
宣告式語法
<asp:ObjectDataSource
CacheDuration="string|Infinite"
CacheExpirationPolicy="Absolute|Sliding"
CacheKeyDependency="string"
ConflictDetection="OverwriteChanges|CompareAllValues"
ConvertNullToDBNull="True|False"
DataObjectTypeName="string"
DeleteMethod="string"
EnableCaching="True|False"
EnablePaging="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
FilterExpression="string"
ID="string"
InsertMethod="string"
MaximumRowsParameterName="string"
OldValuesParameterFormatString="string"
OnDataBinding="DataBinding event handler"
OnDeleted="Deleted event handler"
OnDeleting="Deleting event handler"
OnDisposed="Disposed event handler"
OnFiltering="Filtering event handler"
OnInit="Init event handler"
OnInserted="Inserted event handler"
OnInserting="Inserting event handler"
OnLoad="Load event handler"
OnObjectCreated="ObjectCreated event handler"
OnObjectCreating="ObjectCreating event handler"
OnObjectDisposing="ObjectDisposing event handler"
OnPreRender="PreRender event handler"
OnSelected="Selected event handler"
OnSelecting="Selecting event handler"
OnUnload="Unload event handler"
OnUpdated="Updated event handler"
OnUpdating="Updating event handler"
runat="server"
SelectCountMethod="string"
SelectMethod="string"
SkinID="string"
SortParameterName="string"
SqlCacheDependency="string"
StartRowIndexParameterName="string"
TypeName="string"
UpdateMethod="string"
Visible="True|False"
>
<DeleteParameters>
<asp:ControlParameter
ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter
ConvertEmptyStringToNull="True|False"
CookieName="string"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:FormParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
FormField="string"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:Parameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:ProfileParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:QueryStringParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
QueryStringField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:SessionParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
SessionField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter
ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter
ConvertEmptyStringToNull="True|False"
CookieName="string"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:FormParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
FormField="string"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:Parameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:ProfileParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:QueryStringParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
QueryStringField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:SessionParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
SessionField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
</FilterParameters>
<InsertParameters>
<asp:ControlParameter
ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter
ConvertEmptyStringToNull="True|False"
CookieName="string"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:FormParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
FormField="string"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:Parameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:ProfileParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:QueryStringParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
QueryStringField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:SessionParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
SessionField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
</InsertParameters>
<SelectParameters>
<asp:ControlParameter
ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter
ConvertEmptyStringToNull="True|False"
CookieName="string"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:FormParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
FormField="string"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:Parameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:ProfileParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:QueryStringParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
QueryStringField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:SessionParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
SessionField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter
ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter
ConvertEmptyStringToNull="True|False"
CookieName="string"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:FormParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
FormField="string"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:Parameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:ProfileParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:QueryStringParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
QueryStringField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:SessionParameter
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
SessionField="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
</UpdateParameters>
</asp:ObjectDataSource>
建構函式
ObjectDataSource() |
初始化 ObjectDataSource 類別的新執行個體。 |
ObjectDataSource(String, String) |
使用指定的型別名稱和資料擷取方法名稱,初始化 ObjectDataSource 類別的新執行個體。 |
屬性
Adapter |
針對控制項取得瀏覽器的特定配置器。 (繼承來源 Control) |
AppRelativeTemplateSourceDirectory |
取得或設定包含了此控制項之 Page 或 UserControl 物件的相對應用程式虛擬目錄。 (繼承來源 Control) |
BindingContainer |
取得包含了此控制項之資料繫結的控制項。 (繼承來源 Control) |
CacheDuration |
取得或設定資料來源控制項快取 SelectMethod 屬性擷取之資料的時間長度 (以秒為單位)。 |
CacheExpirationPolicy |
取得或設定快取到期行為,當與持續期間搭配使用時,用來描述資料來源控制項所使用的快取行為。 |
CacheKeyDependency |
取得或設定使用者定義的索引鍵相依性,連結至資料來源控制項所建立的所有資料快取物件。 |
ChildControlsCreated |
取得值,指出是否已經建立伺服器控制項的子控制項。 (繼承來源 Control) |
ClientID |
取得 ASP.NET 產生的伺服器控制項識別項。 (繼承來源 DataSourceControl) |
ClientIDMode |
這個屬性不會用於資料來源控制項。 (繼承來源 DataSourceControl) |
ClientIDSeparator |
取得字元值,表示在 ClientID 屬性中所使用的分隔字元。 (繼承來源 Control) |
ConflictDetection |
取得或設定值,判斷是只有新值傳遞至 |
Context |
取得與目前 Web 要求的伺服器控制項關聯的 HttpContext 物件。 (繼承來源 Control) |
Controls |
取得 ControlCollection 物件,表示 UI 階層架構中指定之伺服器控制項的子控制項。 (繼承來源 DataSourceControl) |
ConvertNullToDBNull |
取得或設定值,指出傳遞至更新、傳入或刪除作業的 Parameter 值是否由 Value 控制項自動從 |
DataItemContainer |
如果命名容器實作 IDataItemContainer,則取得命名容器的參考。 (繼承來源 Control) |
DataKeysContainer |
如果命名容器實作 IDataKeysControl,則取得命名容器的參考。 (繼承來源 Control) |
DataObjectTypeName |
取得或設定類別的名稱,ObjectDataSource 控制項會將該類別用於更新、插入或刪除資料作業中的參數,而不會從資料繫結控制項傳遞個別的值。 |
DeleteMethod |
取得或設定 ObjectDataSource 控制項叫用以刪除資料之方法或函式的名稱。 |
DeleteParameters |
取得包含 DeleteMethod 方法所用參數的參數集合。 |
DesignMode |
取得值,指出控制項是否正用於設計介面上。 (繼承來源 Control) |
EnableCaching |
取得或設定值,指出 ObjectDataSource 控制項是否啟用了資料快取。 |
EnablePaging |
取得或設定值,指出資料來源控制項是否支援對其擷取的資料集進行分頁。 |
EnableTheming |
取得值,指出這個控制項是否支援佈景主題。 (繼承來源 DataSourceControl) |
EnableViewState |
取得或設定值,該值表示伺服器控制項是否對要求的用戶端而言保持其檢視狀態,以及它包含的任何子控制項狀態。 (繼承來源 Control) |
Events |
取得控制項事件處理常式委派 (Delegate) 的清單。 這個屬性是唯讀的。 (繼承來源 Control) |
FilterExpression |
取得或設定呼叫 SelectMethod 屬性所指定之方法時套用的篩選條件運算式。 |
FilterParameters |
取得與 FilterExpression 字串中任何參數替代符號關聯的參數集合。 |
HasChildViewState |
取得值,指出目前伺服器控制項的子控制項是否有任何已儲存的檢視狀態設定。 (繼承來源 Control) |
ID |
取得或設定指派給伺服器控制項的程式設計識別項。 (繼承來源 Control) |
IdSeparator |
取得用來分隔控制項識別項的字元。 (繼承來源 Control) |
InsertMethod |
取得或設定 ObjectDataSource 控制項叫用以插入資料之方法或函式的名稱。 |
InsertParameters |
取得包含 InsertMethod 屬性所使用參數的參數集合。 |
IsChildControlStateCleared |
取得值,指出這個控制項中所包含的控制項是否有控制項狀態。 (繼承來源 Control) |
IsTrackingViewState |
取得值,指出伺服器控制項是否正在儲存檢視狀態的變更。 (繼承來源 Control) |
IsViewStateEnabled |
取得值,指出這個控制項是否已啟用檢視狀態。 (繼承來源 Control) |
LoadViewStateByID |
取得值,指出控制項是否依 ID (而不是索引) 參與載入其檢視狀態。 (繼承來源 Control) |
MaximumRowsParameterName |
取得或設定商務物件資料擷取方法參數的名稱,用於指出為資料來源分頁支援所擷取的資料錄數目。 |
NamingContainer |
取得伺服器控制項命名容器的參考,其建立唯一命名空間,在具有相同 ID 屬性值的伺服器控制項之間作區別。 (繼承來源 Control) |
OldValuesParameterFormatString |
取得或設定格式字串,套用至傳遞給 |
Page |
取得含有伺服器控制項的 Page 執行個體的參考。 (繼承來源 Control) |
Parent |
在網頁控制階層架構中取得伺服器控制項之父控制項的參考。 (繼承來源 Control) |
ParsingCulture |
取得或設定值,指出在將字串值轉換成實際的屬性型別,以建構 DataObjectTypeName 所表示的型別物件時,使用何種文化特性資訊。 |
RenderingCompatibility |
取得值,這個值會指定將與呈現 HTML 相容的 ASP.NET 版本。 (繼承來源 Control) |
SelectCountMethod |
取得或設定 ObjectDataSource 控制項叫用以擷取資料列計數之方法或函式的名稱。 |
SelectMethod |
取得或設定 ObjectDataSource 控制項叫用以擷取資料之方法或函式的名稱。 |
SelectParameters |
取得參數的集合,這些參數是由 SelectMethod 屬性指定的方法所使用。 |
Site |
當呈現在設計介面上時,取得裝載目前控制項之容器的資訊。 (繼承來源 Control) |
SkinID |
取得套用至 DataSourceControl 控制項的面板。 (繼承來源 DataSourceControl) |
SortParameterName |
取得或設定商務物件的名稱,SelectMethod 參數使用該名稱指定資料來源排序支援的排序運算式。 |
SqlCacheDependency |
取得或設定以分號分隔的字串,表示用於 Microsoft SQL Server 快取相依性的資料庫和資料表。 |
StartRowIndexParameterName |
取得或設定資料擷取方法參數的名稱,用於指出為資料來源分頁支援所擷取之第一個資料錄的識別項的值。 |
TemplateControl |
取得或設定包含了此控制項之樣板的參考。 (繼承來源 Control) |
TemplateSourceDirectory |
取得包含目前伺服器控制項的 Page 或 UserControl 的虛擬目錄。 (繼承來源 Control) |
TypeName |
取得或設定 ObjectDataSource 物件表示之類別的名稱。 |
UniqueID |
取得伺服器控制項唯一的、符合階層架構的識別項。 (繼承來源 Control) |
UpdateMethod |
取得或設定 ObjectDataSource 控制項叫用以更新資料之方法或函式的名稱。 |
UpdateParameters |
取得參數集合,包含 UpdateMethod 屬性指定之方法所使用的參數。 |
ValidateRequestMode |
取得或設定值,指出控制項是否對來自瀏覽器的用戶端輸入檢查潛在的危險值。 (繼承來源 Control) |
ViewState |
取得狀態資訊的字典,允許您在相同網頁的多個要求之間,儲存和還原伺服器控制項的檢視狀態。 (繼承來源 Control) |
ViewStateIgnoresCase |
取得值,指出 StateBag 物件是否不區分大小寫。 (繼承來源 Control) |
ViewStateMode |
取得或設定這個控制項的檢視狀態模式。 (繼承來源 Control) |
Visible |
取得或設定值,指出是否視覺化顯示控制項。 (繼承來源 DataSourceControl) |
方法
事件
DataBinding |
發生於伺服器控制項繫結至資料來源時。 (繼承來源 Control) |
Deleted |
發生於 Delete() 作業已經完成時。 |
Deleting |
在 Delete() 作業之前發生。 |
Disposed |
發生於伺服器控制項從記憶體釋放時,這是在要求 ASP.NET 網頁時,伺服器控制項生命週期的最後階段。 (繼承來源 Control) |
Filtering |
在篩選作業之前發生。 |
Init |
發生於初始化伺服器控制項時,是其生命週期中的第一個步驟。 (繼承來源 Control) |
Inserted |
發生於 Insert() 作業已經完成時。 |
Inserting |
在 Insert() 作業之前發生。 |
Load |
發生於載入伺服器控制項至 Page 物件時。 (繼承來源 Control) |
ObjectCreated |
在建立 TypeName 屬性識別的物件之後發生。 |
ObjectCreating |
在建立 TypeName 屬性識別的物件之前發生。 |
ObjectDisposing |
在捨棄 TypeName 屬性識別的物件之前發生。 |
PreRender |
在 Control 物件載入之後但在呈現之前發生。 (繼承來源 Control) |
Selected |
發生於 Select() 作業已經完成時。 |
Selecting |
在 Select() 作業之前發生。 |
Unload |
發生於伺服器控制項從記憶體卸載時。 (繼承來源 Control) |
Updated |
發生於 Update() 作業已經完成時。 |
Updating |
在 Update() 作業之前發生。 |
明確介面實作
擴充方法
FindDataSourceControl(Control) |
傳回與指定之控制項的資料控制項相關聯的資料來源。 |
FindFieldTemplate(Control, String) |
傳回在指定之控制項的命名容器中所指定資料行的欄位樣板。 |
FindMetaTable(Control) |
傳回包含資料控制項的中繼資料表物件。 |
GetDefaultValues(IDataSource) |
取得所指定資料來源的預設值集合。 |
GetMetaTable(IDataSource) |
取得所指定資料來源物件中的資料表中繼資料。 |
TryGetMetaTable(IDataSource, MetaTable) |
判斷資料表中繼資料是否可供使用。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應