SqlDataSource.FilterExpression 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定當呼叫 Select(DataSourceSelectArguments) 時會套用的篩選運算式。
public:
property System::String ^ FilterExpression { System::String ^ get(); void set(System::String ^ value); };
public string FilterExpression { get; set; }
member this.FilterExpression : string with get, set
Public Property FilterExpression As String
屬性值
字串,代表當使用 Select(DataSourceSelectArguments) 方法抓取資料時套用的篩選運算式。
例外狀況
已設定 FilterExpression 屬性,且 SqlDataSource 處於 DataReader 模式中。
範例
下列程式碼範例示範如何從 Northwind 資料庫擷取資料,並使用字串和 FilterParameters 集合加以篩選 FilterExpression 。 FilterExpression每當方法執行以擷取資料時, Select 就會套用 屬性。 在此範例中 FilterExpression ,包含篩選參數的預留位置,包含在集合中 FilterParameters 。 此外,filter 參數是 ControlParameter 系結至 SelectedValue 控制項之 屬性的物件 DropDownList 。 DropDownList因為控制項的 AutoPostBack 屬性設定為 true
,所以控制項的選取範圍 DropDownList 中的任何變更都會讓頁面將資訊張貼回伺服器,而 GridView 控制項會使用新的篩選準則重新系結至資料來源控制項。
<!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>
<!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 從執行 Select 方法傳回之 物件的 屬性 DataView 。 如需詳細資訊,請參閱Expression。
如果您將參數新增至 FilterParameters 集合,您也可以在運算式中包含格式字串預留位置 (, "{0}")
以取代參數值。 預留位置會根據集合中 FilterParameters 參數的索引來取代。 如果集合中的 FilterParameters 物件為 null
,則會以空字串取代物件。
您可以在 屬性中包含 FilterExpression 參數。 如果參數是字串或字元類型,請將參數括在單引號中。 如果參數是數數值型別,則不需要引號。 FilterParameters集合包含針對 屬性中找到 FilterExpression 的預留位置評估的參數。
只有在模式中 DataSet 時,控制項 SqlDataSource 才支援篩選資料。
屬性 FilterExpression 會委派給 FilterExpression 與 SqlDataSource 控制項相關聯之 物件的 屬性 SqlDataSourceView 。