Export (0) Print
Expand All

ObjectDataSourceView.SelectMethod Property

Gets or sets the name of the method or function that the ObjectDataSourceView control invokes to retrieve data.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public string SelectMethod { get; set; }

Property Value

Type: System.String
A string that represents the name of the method or function that the ObjectDataSourceView uses to retrieve data. The default is an empty string ("").

The specified method can have any method signature but must return one of the types listed in the following table in order for the ObjectDataSource control to call it successfully

Return type

Action

IEnumerable

The IEnumerable is returned by the Select method.

DataTable

A DataView is created using the DataTable and returned by the Select method.

DataSet

The first DataTable of the DataSet is extracted and a DataView is created and returned by the Select method.

Object

The object is wrapped in a one-element IEnumerable and returned by the Select method.

The method that is identified by the SelectMethod property can be an instance method or a static (Shared in Visual Basic) method. If it is an instance method, the business object is created and destroyed each time the SelectMethod method is called. You can handle the ObjectCreated event to work with the business object before the method specified by the SelectMethod property is called. You can also handle the ObjectDisposing event that is raised after the method specified by the SelectMethod property is called. (Dispose is called only if the business object implements the IDisposable interface.) If the method is a static (Shared in Visual Basic) method, the business object is never created and you cannot handle these events.

If the business object that the ObjectDataSource control works with implements more than one method or function with the same name (method overloads), the data source control attempts to invoke the correct one according to a set of conditions, including the parameters in the SelectParameters collection. If the parameters in the SelectParameters collection do not match those of the signature of the method specified by the SelectMethod property, the data source throws an exception.

For more information, see ObjectDataSource.SelectMethod.

The following code example demonstrates how a GridView control can display data using an ObjectDataSource control on a Web Forms page. The 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. The GridView control enumerates through the IEnumerable collection that is returned by the SelectMethod, and then displays the data.


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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft