Redigera

Dela via


ObjectDataSource.SelectParameters Property

Definition

Gets a collection of parameters that are used by the method specified by the SelectMethod property.

public:
 property System::Web::UI::WebControls::ParameterCollection ^ SelectParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection SelectParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.SelectParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property SelectParameters As ParameterCollection

Property Value

A collection of parameters that are used by the method specified by the SelectMethod property.

Attributes

Examples

The following code example demonstrates how to retrieve a single data record using an ObjectDataSource control and display it in a DetailsView control. The ObjectDataSource control retrieves a specific employee record by calling the GetEmployee method of the EmployeeLogic class. The GetEmployee method requires an employee ID parameter. The ObjectDataSource control uses a QueryStringParameter element in markup to create a QueryStringParameter object that will retrieve the value of a specified query-string parameter from the URL and pass it to the GetEmployee method.

Other parameter classes can be used to retrieve values from other sources, such as control properties, form fields, cookies, ASP.NET profile data, or session state. You can also use a parameter object that is defined in markup in order to pass a hard-coded value to the select method. For more information about parameter classes that are available, see Using Parameters with Data Source Controls for Filtering.

To run this example, you will need the following:

  • A Web site that has a connection string named NorthwindConnection that connects to the Northwind Traders database. For more information, see the ObjectDataSource class overview.

  • The EmployeeLogic class that is shown as one of the code examples for the ObjectDataSource class overview. The class file must be located in the App_Code folder.

  • The sample code can be run as an .aspx page. If you request the page without a query-string parameter, an error message is displayed. If you request the page using a query string such as ?empid=1 after the name of the .aspx file, you will see the details for the requested employee.

<%@ 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:detailsview
          id="DetailsView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:detailsview>

<!-- Security Note: The ObjectDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetEmployee"
          typename="Samples.AspNet.CS.EmployeeLogic" >
          <selectparameters>
            <asp:querystringparameter name="EmployeeID" querystringfield="empid" defaultvalue="-1" />
          </selectparameters>
        </asp:objectdatasource>

    </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 - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:detailsview
          id="DetailsView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:detailsview>

<!-- Security Note: The ObjectDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetEmployee"
          typename="Samples.AspNet.VB.EmployeeLogic" >
          <selectparameters>
            <asp:querystringparameter name="EmployeeID" querystringfield="empid" defaultvalue="-1" />
          </selectparameters>
        </asp:objectdatasource>

    </form>
  </body>
</html>

Remarks

The SelectParameters property gets the SelectParameters property of the ObjectDataSourceView object that is associated with the ObjectDataSource control.

You add parameters to the SelectParameters collection declaratively by using the SelectParameters element or programmatically in the handler for the Selecting event. At run time, parameters listed in the SelectParameters element are added to the collection first. Parameters in the collection are then added or removed by the handler for the Selecting event. The Selecting event is raised before the Select method is run.

Before the Select method is run, the names and types of the parameters that are contained in the SelectParameters collection must match the signature of the method that is specified by the SelectMethod property. For example, if a select method named GetEmployeesByStateAndAge takes a string and an integer as parameters, the SelectParameters collection must contain two parameters. The first parameter must resolve to a string and the second parameter must resolve to an integer. Both parameters can be specified in markup in the SelectParameters element. Alternatively, they can be can be added in the Selecting event handler, or one parameter can be added in markup and the other one can be added programmatically.

For more information, see Using Parameters with the ObjectDataSource Control and the SelectMethod property.

Important

In the code for the Selecting event handler or in the method that is specified by the SelectMethod property, make sure that you validate any parameter value that is received from the client.

Applies to

See also