Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

ObjectDataSource.SelectMethod Property

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

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public string SelectMethod { get; set; }
<asp:ObjectDataSource SelectMethod="String" />

Property Value

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

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

The IEnumerable is returned by the Select method.

DataTable

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

DataView

A DataView is 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 collection and returned by the Select method.

The SelectMethod property delegates to the SelectMethod property of the ObjectDataSourceView object that is associated with the ObjectDataSource control.

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

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 method that is specified by the SelectMethod property is called. 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. You can also handle the ObjectDisposing event that is raised after the method that is specified by the SelectMethod property is called. If the business object implements the IDisposable interface, the Dispose method is called before the object is destroyed. 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

Parameters are added to the SelectParameters collection from these sources:

  • Declaratively from the SelectParameters element.

  • Programmatically from the Selecting method.

First, the parameters listed in the SelectParameters element are added. 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.

Security noteSecurity Note

You should validate any parameter value that you receive from the client. The runtime simply substitutes the parameter value into the SelectMethod property.

Method Resolution

When 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. (For more information, see the preceding section.) The ObjectDataSource control then attempts to find a method to call. First, it looks for one or more methods with the name that is specified in the SelectMethod property. If no match is found, an InvalidOperationException exception is thrown. If a match is found, it then looks for matching parameter names. For example, suppose a type that is specified by the TypeName property has two methods named SelectARecord. One SelectARecord has one parameter, ID, and the other SelectARecord has two parameters, Name and Number. If 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.

If the DataObjectTypeName property is set, the method is resolved in a different way. The 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.

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.