ObjectDataSource.SelectParameters 属性

定义

获取参数的集合,这些参数由 SelectMethod 属性指定的方法使用。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; }
member this.SelectParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property SelectParameters As ParameterCollection

属性值

参数的集合,这些参数由 SelectMethod 属性指定的方法使用。A collection of parameters that are used by the method specified by the SelectMethod property.

属性

示例

下面的代码示例演示如何使用ObjectDataSource控件检索单个数据记录并将其显示DetailsView在控件中。The following code example demonstrates how to retrieve a single data record using an ObjectDataSource control and display it in a DetailsView control. 控件通过调用类的GetEmployee EmployeeLogic方法来检索特定的雇员记录。 ObjectDataSourceThe ObjectDataSource control retrieves a specific employee record by calling the GetEmployee method of the EmployeeLogic class. GetEmployee方法需要雇员 ID 参数。The GetEmployee method requires an employee ID parameter. 控件使用标记中QueryStringParameter的元素来创建一个QueryStringParameter对象, 该对象将GetEmployee从 URL 中检索指定的查询字符串参数的值并将其传递给方法。 ObjectDataSourceThe 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.

其他参数类可用于检索来自其他源的值, 例如控件属性、窗体字段、cookie、ASP.NET 配置文件数据或会话状态。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. 你还可以使用在标记中定义的参数对象, 以便将硬编码值传递给 select 方法。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:

  • 一个网站, 该网站具有连接到 Northwind NorthwindConnection商贸数据库的名为的连接字符串。A Web site that has a connection string named NorthwindConnection that connects to the Northwind Traders database. 有关详细信息, 请参阅ObjectDataSource类概述。For more information, see the ObjectDataSource class overview.

  • 类, 它显示为ObjectDataSource类概述的代码示例之一。 EmployeeLogicThe EmployeeLogic class that is shown as one of the code examples for the ObjectDataSource class overview. 类文件必须位于 App_Code 文件夹中。The class file must be located in the App_Code folder.

  • 示例代码可作为 .aspx 页运行。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. 如果使用查询字符串 ( ?empid=1如 .aspx 文件的名称之后) 请求页面, 则会看到请求的雇员的详细信息。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>

注解

ObjectDataSourceView属性获取与ObjectDataSource控件关联的对象的属性。SelectParameters SelectParametersThe SelectParameters property gets the SelectParameters property of the ObjectDataSourceView object that is associated with the ObjectDataSource control.

您可以SelectParameters SelectParameters使用元素以声明方式向集合添加参数, 或Selecting在事件的处理程序中以编程方式向集合添加参数。You add parameters to the SelectParameters collection declaratively by using the SelectParameters element or programmatically in the handler for the Selecting event. 在运行时, 元素中列出的SelectParameters参数首先添加到集合中。At run time, parameters listed in the SelectParameters element are added to the collection first. 然后, 将在该Selecting事件的处理程序中添加或删除集合中的参数。Parameters in the collection are then added or removed by the handler for the Selecting event. 此事件在Select方法运行前引发。 SelectingThe Selecting event is raised before the Select method is run.

在运行SelectParameters方法之前, 集合中包含的参数的名称和类型必须与SelectMethod属性指定的方法的签名相匹配。 SelectBefore 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. 例如, 如果名为GetEmployeesByStateAndAge的 select 方法采用 a string和 integer 作为参数, 则SelectParameters集合必须包含两个参数。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. 可在元素的SelectParameters标记中指定这两个参数。Both parameters can be specified in markup in the SelectParameters element. 或者, 可以将它们添加到Selecting事件处理程序中, 也可以在标记中添加一个参数, 并以编程方式添加另一个参数。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.

有关详细信息, 请参阅将参数用于 ObjectDataSource 控件SelectMethod属性。For more information, see Using Parameters with the ObjectDataSource Control and the SelectMethod property.

重要

Selecting事件处理程序的代码或SelectMethod属性指定的方法中, 确保验证从客户端接收的所有参数值。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.

适用于

另请参阅