ObjectDataSource.SelectMethod ObjectDataSource.SelectMethod ObjectDataSource.SelectMethod ObjectDataSource.SelectMethod Property

定义

获取或设置 ObjectDataSource 控件调用以检索数据的方法或函数的名称。Gets or sets the name of the method or function that the ObjectDataSource control invokes to retrieve data.

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

属性值

一个字符串,表示 ObjectDataSource 用于检索数据的方法或函数的名称。A string that represents the name of the method or function that the ObjectDataSource uses to retrieve data. 默认值为空字符串("")。The default is an empty string ("").

示例

下面的代码示例演示GridView控件如何使用 Web 窗体页上的ObjectDataSource控件来显示数据。The following code example demonstrates how a GridView control can display data using an ObjectDataSource control on a Web Forms page. 标识部分或完全限定类名TypeName及其属性和调用以检索数据及其SelectMethod属性的方法。 ObjectDataSourceThe ObjectDataSource identifies a partially or fully qualified class name with its TypeName property and a method that is called to retrieve data with its SelectMethod property. 在运行时, 将创建对象并使用反射调用方法。At run time, the object is created and the method is called using reflection. 控件枚举SelectMethod由属性指定的方法返回的集合,并显示数据。IEnumerable GridViewThe GridView control enumerates through the IEnumerable collection that is returned by the method that is specified by the SelectMethod property, and displays the data.

<%@ 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控件成功调用它。The specified method can have any method signature, but it must return one of the types shown in the following table for the ObjectDataSource control to call it successfully.

返回类型Return type 操作Action
IEnumerable IEnumerableSelect方法返回。The IEnumerable is returned by the Select method.
DataTable 使用方法创建并返回。 DataTable DataView SelectA DataView is created using the DataTable and returned by the Select method.
DataView 方法返回SelectDataViewA DataView is returned by the Select method.
DataSet 第一个DataTable DataSet提取, 并DataViewSelect方法创建并返回。The first DataTable of the DataSet is extracted, and a DataView is created and returned by the Select method.
Object 对象包装在一个单元素IEnumerable集合中, 并Select由方法返回。The object is wrapped in a one-element IEnumerable collection and returned by the Select method.

属性委托ObjectDataSourceView SelectMethod给与ObjectDataSource控件相关联的对象的属性。 SelectMethodThe SelectMethod property delegates to the SelectMethod property of the ObjectDataSourceView object that is associated with the ObjectDataSource control.

使用ObjectDataSource类删除或更新数据时, 请确保为DeleteParameters集合或UpdateParameters集合中的ObjectDataSource控件配置的参数名称与 select 方法返回的列名称相匹配.When you use the ObjectDataSource class to delete or update data, make sure that the parameter names configured for the ObjectDataSource control in the DeleteParameters collection or UpdateParameters collection match the column names that are returned by the select method.

对象生存期Object Lifetime

SelectMethod属性标识的方法可以是实例方法, static也可以是 (Shared在 Visual Basic) 方法。The method that is identified by the SelectMethod property can be an instance method or a static (Shared in Visual Basic) method. 如果它是实例方法, 则每次调用由SelectMethod属性指定的方法时都会创建并销毁业务对象。If it is an instance method, the business object is created and destroyed each time the method that is specified by the SelectMethod property is called. 在调用由ObjectCreated ObjectCreating 属性指定的方法之前,您可以处理和事件来SelectMethod处理业务对象。You can handle the ObjectCreated and ObjectCreating events to work with the business object before the method that is specified by the SelectMethod property is called. 你还可以处理在ObjectDisposing调用SelectMethod由属性指定的方法之后引发的事件。You can also handle the ObjectDisposing event that is raised after the method that is specified by the SelectMethod property is called. 如果业务对象实现IDisposable接口Dispose , 则在销毁对象之前调用方法。If the business object implements the IDisposable interface, the Dispose method is called before the object is destroyed. 如果该方法为static (Shared在 Visual Basic 中), 则永远不会创建业务对象, 也不ObjectCreatedObjectCreating处理、 ObjectDisposing和事件。If the method is static (Shared in Visual Basic), the business object is never created and you cannot handle the ObjectCreated, ObjectCreating, and ObjectDisposing events.

参数合并Parameter Merging

将从这些源向SelectParameters集合添加参数:Parameters are added to the SelectParameters collection from these sources:

  • SelectParameters元素以声明方式。Declaratively from the SelectParameters element.

  • 以编程方式Selecting从方法。Programmatically from the Selecting method.

首先, 添加SelectParameters元素中列出的参数。First, the parameters listed in the SelectParameters element are added. 其次, 参数在Selecting事件中以编程方式添加和移除, 这在Select方法运行之前发生。Second, parameters are programmatically added and removed in the Selecting event, which occurs before the Select method is run. 合并参数后, 解析方法。The method is resolved after the parameters are merged. 下一节将讨论方法解析。Method resolution is discussed in the next section.

重要

你应验证从客户端接收的任何参数值。You should validate any parameter value that you receive from the client. 运行时只需将参数值替换为SelectMethod属性。The runtime simply substitutes the parameter value into the SelectMethod property.

方法解析Method Resolution

调用方法时, 数据绑定控件中的数据字段、在SelectParameters元素中以声明方式创建的参数以及在Selecting事件处理程序中添加的参数都将合并。 SelectWhen the Select method is called, the data fields from the data-bound control, the parameters that were created declaratively in the SelectParameters element, and the parameters that were added in the Selecting event handler are all merged. (有关详细信息, 请参阅前面的部分。)然后ObjectDataSource , 控件尝试查找要调用的方法。(For more information, see the preceding section.) The ObjectDataSource control then attempts to find a method to call. 首先, 它会查找一个或多个具有在SelectMethod属性中指定的名称的方法。First, it looks for one or more methods with the name that is specified in the SelectMethod property. 如果未找到匹配项, InvalidOperationException则会引发异常。If no match is found, an InvalidOperationException exception is thrown. 如果找到匹配项, 则它将查找匹配的参数名称。If a match is found, it then looks for matching parameter names. 例如, 假设TypeName属性指定的类型有两个名为SelectARecord的方法。For example, suppose a type that is specified by the TypeName property has two methods named SelectARecord. 一个SelectARecord SelectARecord 参数为,另Name一个参数Number具有两个参数: 和。 IDOne SelectARecord has one parameter, ID, and the other SelectARecord has two parameters, Name and Number. 如果集合仅包含一个名为ID的参数, SelectARecord则将调用只ID包含参数的方法。 SelectParametersIf the SelectParameters collection has only one parameter named ID, the SelectARecord method with just the ID parameter is called. 解析方法时未检查参数类型。The type of the parameter is not checked in resolving the methods. 参数的顺序并不重要。The order of the parameters does not matter.

如果设置了属性, 则会以不同的方式解析方法。 DataObjectTypeNameIf the DataObjectTypeName property is set, the method is resolved in a different way. 查找具有在属性中指定的名称的方法, 该属性采用在DataObjectTypeName属性中指定的类型的一个参数。 SelectMethod ObjectDataSourceThe ObjectDataSource looks for a method with the name that is specified in the SelectMethod property that takes one parameter of the type that is specified in the DataObjectTypeName property. 在这种情况下, 参数名称并不重要。In this case, the name of the parameter does not matter.

适用于

另请参阅