Share via


ObjectDataSourceView.SelectMethod 屬性

定義

取得或設定 ObjectDataSourceView 控制項叫用以擷取資料之方法或函式的名稱。

public:
 property System::String ^ SelectMethod { System::String ^ get(); void set(System::String ^ value); };
public string SelectMethod { get; set; }
member this.SelectMethod : string with get, set
Public Property SelectMethod As String

屬性值

字串,表示 ObjectDataSourceView 用於擷取資料之方法或函式的名稱。 預設為空字串 ("")。

範例

下列程式碼範例示範控制項如何使用 GridView 控制項在Web Form頁面上顯示資料 ObjectDataSource 。 會 ObjectDataSource 使用其 TypeName 屬性識別部分或完整類別名稱,以及呼叫以使用其 SelectMethod 屬性擷取資料的方法。 在執行時間,會建立 物件,並使用反映呼叫 方法。 控制項 GridView 會透過 IEnumerableSelectMethod 傳回的集合列舉,然後顯示資料。

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

備註

指定的方法可以有任何方法簽章,但必須傳回下表所列的其中一種類型,控制項 ObjectDataSource 才能成功呼叫它

傳回類型 動作
IEnumerable 方法 IEnumerable 會傳 Select 回 。
DataTable DataView會使用 方法 DataTable 所傳回的 Select 來建立 。
DataSet 會擷取第 DataSet 一個 DataTableDataView ,並由 方法建立和傳 Select 回 。
Object 物件會包裝在一個專案 IEnumerable 中,並由 方法傳 Select 回。

屬性所 SelectMethod 識別的方法可以是 Visual Basic) 方法中的實例方法或 static (Shared 。 如果是實例方法,每次呼叫方法時 SelectMethod ,都會建立並終結商務物件。 您可以在呼叫 屬性所 SelectMethod 指定的 方法之前,先處理 ObjectCreated 事件以使用商務物件。 您也可以處理 ObjectDisposing 呼叫 屬性所 SelectMethod 指定方法之後所引發的事件。 Dispose只有在商務物件實作 IDisposable interface.) 如果方法是 static Visual Basic) 方法中的 (Shared ,則永遠不會建立商務物件,而且您無法處理這些事件時,才會呼叫 (。

如果控制項使用的商務物件 ObjectDataSource 會實作多個名稱相同的方法或函式, (方法多載) ,則資料來源控制項會嘗試根據一組條件叫用正確的方法,包括集合中的 SelectParameters 參數。 如果集合中的 SelectParameters 參數不符合 屬性所 SelectMethod 指定方法的簽章,則資料來源會擲回例外狀況。

如需詳細資訊,請參閱ObjectDataSource.SelectMethod

適用於

另請參閱