ObjectDataSource.FilterParameters 属性

定义

获取与 FilterExpression 字符串中的任何参数占位符关联的参数的集合。

[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection FilterParameters { get; }

属性值

ParameterCollection,它包含与在 FilterExpression 属性中找到的任何参数占位符相关联的一组参数。

属性

例外

您设置了 FilterExpression 属性,但 Select() 方法没有返回 DataSetDataTable

示例

本部分包含两个代码示例。 第一个 ObjectDataSource 代码示例演示如何使用 对象来从中间层业务对象检索数据,并使用 GridView 控件来显示筛选的数据,以便显示结果。 第二个代码示例提供了在第一个代码示例中使用的中间层业务对象的示例。

下面的代码示例演示了如何使用 控件来从中间层业务对象检索数据,以及GridView使用 控件显示结果来显示筛选ObjectDataSource的数据。 ObjectDataSource仅当检索数据的方法以 或 DataTable 对象的形式DataSet检索数据时,控件才能筛选数据。 出于此原因, SelectMethod 属性标识一个业务对象方法,该方法以 的形式 DataSet检索数据。

代码示例由控件TextBox、控件、GridViewObjectDataSource控件和“提交”按钮组成。 默认情况下, TextBox 使用 Northwind Traders 员工之一的姓名填充 。 显示 GridView 有关员工的信息,这些信息由 中 TextBox的名称标识。 若要检索另一名员工的数据,请在 TextBox中输入该员工的全名,然后单击“ 提交 ”按钮。

属性 FilterExpression 指定用于筛选属性检索的数据的 SelectMethod 表达式。 它使用参数占位符,这些占位符的计算结果为集合中包含的 FilterParameters 参数。 在此示例中,参数占位符由单引号绑定,因为参数的类型是可能包含空格的字符串类型。 如果参数的类型是数值或日期类型,则不需要带引号。 集合 FilterParameters 包含一个参数,即 FormParameter 绑定到 控件的对象 TextBox

重要

应验证从客户端接收的任何筛选器参数值。 运行时只是将参数值替换到筛选器表达式中,并将其应用于 DataView 方法返回 Select 的对象。 如果使用 FilterExpression 属性作为安全措施来限制返回的项数,则必须在筛选之前验证参数值。

<%@ 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">
<script runat="server">

    protected void ObjectDataSource1_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
    {
        if (Textbox1.Text == "")
        {
            e.ParameterValues.Clear();
            e.ParameterValues.Add("FullName", "Nancy Davolio");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <p>Show all users with the following name.</p>

        <asp:textbox id="Textbox1" runat="server" text="Nancy Davolio" />

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1"
          autogeneratecolumns="False">
          <columns>
            <asp:boundfield headertext="ID" datafield="EmpID" />
            <asp:boundfield headertext="Name" datafield="FullName" />
            <asp:boundfield headertext="Street Address" datafield="Address" />
          </columns>
        </asp:gridview>

        <!-- Security Note: The ObjectDataSource uses a FormParameter,
             Security Note: which does not perform validation of input from the client. -->

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployeesAsDataSet"
          typename="Samples.AspNet.CS.EmployeeLogic"
          filterexpression="FullName='{0}'" OnFiltering="ObjectDataSource1_Filtering">
            <filterparameters>
              <asp:formparameter name="FullName" formfield="Textbox1" defaultvalue="Nancy Davolio" />
            </filterparameters>
        </asp:objectdatasource>

        <p><asp:button id="Button1" runat="server" text="Search" /></p>

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

下面的代码示例提供了上述代码示例使用的中间层业务对象的示例。 该代码示例由两个基本类组成:

  • EmployeeLogic ,它是封装业务逻辑的类。

  • NorthwindEmployee ,它是一个模型类,仅包含从数据层加载和保存数据所需的基本功能。

为简单起见, EmployeeLogic 类创建一组静态数据,而不是从数据层检索数据。 此示例也很有用,因为示例依赖于你提供 Northwind Traders 员工的完整名称来演示筛选。 对于完整的工作示例,必须编译这些类,并将其与提供的 Web 窗体代码示例一起使用。

namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
  //
  // EmployeeLogic is a stateless business object that encapsulates 
  // the operations you can perform on a NorthwindEmployee object.
  //
  public class EmployeeLogic {

    // Returns a collection of NorthwindEmployee objects.
    public static ICollection GetAllEmployees () {
      ArrayList data = new ArrayList();
           
      data.Add(new NorthwindEmployee(1,"Nancy","Davolio","507 - 20th Ave. E. Apt. 2A"));
      data.Add(new NorthwindEmployee(2,"Andrew","Fuller","908 W. Capital Way"));
      data.Add(new NorthwindEmployee(3,"Janet","Leverling","722 Moss Bay Blvd."));
      data.Add(new NorthwindEmployee(4,"Margaret","Peacock","4110 Old Redmond Rd."));
      data.Add(new NorthwindEmployee(5,"Steven","Buchanan","14 Garrett Hill"));
      data.Add(new NorthwindEmployee(6,"Michael","Suyama","Coventry House Miner Rd."));
      data.Add(new NorthwindEmployee(7,"Robert","King","Edgeham Hollow Winchester Way"));
      
      return data;
    }
    
    public static NorthwindEmployee GetEmployee(object anID) {
      ArrayList data = GetAllEmployees() as ArrayList;     
      int empID = Int32.Parse(anID.ToString());      
      return data[empID] as NorthwindEmployee;
    }

    // 
    // To support basic filtering, the employees cannot
    // be returned as an array of objects, rather as a 
    // DataSet of the raw data values. 
    public static DataSet GetAllEmployeesAsDataSet () {
      ICollection employees = GetAllEmployees();
      
      DataSet ds = new DataSet("Table");
      
      // Create the schema of the DataTable.
      DataTable dt = new DataTable();
      DataColumn dc;
      dc = new DataColumn("EmpID",   typeof(int));    dt.Columns.Add(dc);
      dc = new DataColumn("FullName",typeof(string)); dt.Columns.Add(dc);
      dc = new DataColumn("Address", typeof(string)); dt.Columns.Add(dc);
      
      // Add rows to the DataTable.
      DataRow row;
            
      foreach (NorthwindEmployee ne in employees) {                
        row = dt.NewRow();
        row["EmpID"]    = ne.EmpID;
        row["FullName"] = ne.FullName;
        row["Address"]  = ne.Address;
        dt.Rows.Add(row);
      } 
      // Add the complete DataTable to the DataSet.
      ds.Tables.Add(dt);
      
      return ds;
    }    
  }

  public class NorthwindEmployee {

    public NorthwindEmployee (int anID, 
                              string aFirstName,
                              string aLastName,
                              string anAddress) {
      ID = anID;
      firstName = aFirstName;
      lastName = aLastName;   
      address = anAddress;
    }

    private object ID;
    public string EmpID {
      get { return ID.ToString();  }
    }

    private string lastName;
    public string LastName {
      get { return lastName; }
      set { lastName = value; }
    }

    private string firstName;
    public string FirstName {
      get { return firstName; }
      set { firstName = value;  }
    }
    
    public string FullName {
      get { return FirstName  + " " +  LastName; }
    }
    
    private string address;
    public string Address {
      get { return address; }
      set { address = value;  }
    }    
  }
}

注解

仅当 方法返回 DataSetDataTable 对象时, Select 控件ObjectDataSource才支持筛选数据。

用于 FilterExpression 的语法是格式字符串样式表达式。 筛选器表达式语法与 属性接受 Expression 的语法相同。 如果将参数添加到集合, FilterParameters 还可以包含格式字符串占位符。 例如, "{0}" 在表达式中包含 以替换参数值。 根据集合中 FilterParameters 参数的索引替换占位符。

可以在 属性中包含 FilterExpression 参数。 如果参数是字符串或字符类型,请将参数括在单引号中。 如果 参数是数值类型,则不需要引号。

属性 FilterParameters 检索 FilterParametersObjectDataSourceView 与 控件关联的 对象包含的属性 ObjectDataSource

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅