SqlDataSource.FilterExpression Property

定义

命名空间: System.Web.UI.WebControls Assembly: System.Web.dll


获取或设置调用 Select(DataSourceSelectArguments) 方法时应用的筛选表达式。

C#复制

public string FilterExpression { get; set; }

属性值

String

表示使用 Select(DataSourceSelectArguments) 方法检索数据时应用的筛选表达式的字符串。


异常

NotSupportedException

已设置 FilterExpression​ 属性,且 SqlDataSource​ 处于 DataReader 模式。


示例

下面的代码示例演示如何从 Northwind 数据库中检索数据和筛选使用其FilterExpression​字符串和FilterParameters​集合。FilterExpression​属性应用任何时间Select​执行方法以检索数据。 在此示例中,FilterExpression​包含一个筛选器参数,它包含在一个占位符FilterParameters​集合。 此外,筛选器参数是ControlParameter​绑定到的对象SelectedValue​属性的DropDownList​控件。 因为DropDownList​控件具有其AutoPostBack​属性设置为​​true​​​,为所选内容中的任何更改DropDownList​控制导致页面发布回服务器的信息和GridView控件重新绑定到数据源使用新的筛选器来控制。

ASP.NET (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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">

<p>Show all employees with the following title:
<asp:DropDownList
id="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem Selected="True">Sales Representative</asp:ListItem>
<asp:ListItem>Sales Manager</asp:ListItem>
<asp:ListItem>Vice President, Sales</asp:ListItem>
</asp:DropDownList></p>

<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}'">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource>

<p><asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<columns>
<asp:BoundField Visible="False" DataField="EmployeeID" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</columns>
</asp:GridView></p>

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

注解

FilterExpression​属性值是一个格式字符串表达式 (一个字符串,由处理String.Format​方法),它使用中的值FilterExpression​字符串中包含任何替换参数的集合。 筛选器表达式语法是相同的语法通过接受RowFilter​属性,因为对其应用筛选器表达式RowFilter​的属性DataView​执行返回的对象Select​方法。 有关详情,请参阅Expression。

如果您将参数添加到FilterParameters​集合中,您还可以包含格式字符串的占位符 (例如,​​"{0}")​​​中要替换的参数值的表达式。 根据中的参数的索引替换占位符FilterParameters​集合。 如果中的对象FilterParameters​集合是​​null​​,该对象将替换为空字符串。

可以包括在参数FilterExpression​属性。 如果参数是字符串或字符类型,将参数括在单引号内。 如果参数的数值类型,则不需要,引号引起来。 FilterParameters​集合中包含的参数,其计算结果中找到占位符FilterExpression属性。

SqlDataSource​控件支持筛选数据时,才在DataSet模式。

FilterExpression​属性委托给FilterExpression​的属性SqlDataSourceView​与之关联的对象SqlDataSource控件。

适用于

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0