LinqDataSource.WhereParameters 属性
定义
获取用于创建 Where 子句的参数集合。Gets the collection of parameters that is used to create the Where clause.
public:
property System::Web::UI::WebControls::ParameterCollection ^ WhereParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection WhereParameters { get; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.WhereParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property WhereParameters As ParameterCollection
属性值
用于创建 Where 属性中 Where 子句的参数的集合。A collection of the parameters that are used to create the Where clause in the Where property.
实现
- 属性
示例
下面的示例演示如何使用 WhereParameters 集合动态创建 Where 子句。The following example shows how to use the WhereParameters collection to dynamically create the Where clause. LinqDataSource控件返回的所有记录的列中的值 Price 大于用户在控件中选择的值 DropDownList 。The LinqDataSource control returns all the records with a value in the Price column that is greater than the value selected by the user in a DropDownList control.
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="25"></asp:ListItem>
<asp:ListItem Value="100"></asp:ListItem>
<asp:ListItem Value="400"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
Where="Price>@UserPrice"
ID="LinqDataSource1"
runat="server">
<WhereParameters>
<asp:ControlParameter
Name="UserPrice"
DefaultValue="0"
ControlID="DropDownList1"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:GridView
DataSourceID="LinqDataSource1"
ID="GridView1"
runat="server">
</asp:GridView>
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="25"></asp:ListItem>
<asp:ListItem Value="100"></asp:ListItem>
<asp:ListItem Value="400"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
Where="Price > @UserPrice"
ID="LinqDataSource1"
runat="server">
<WhereParameters>
<asp:ControlParameter
Name="UserPrice"
DefaultValue="0"
ControlID="DropDownList1"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:GridView
DataSourceID="LinqDataSource1"
ID="GridView1"
runat="server">
</asp:GridView>
注解
LinqDataSource控件使用集合中的参数 WhereParameters 在运行时创建 Where 子句。The LinqDataSource control uses parameters in the WhereParameters collection to create the Where clause at run time. WhereParameters当要以编程方式在 Where 子句中设置一个或多个条件时,可以将参数添加到集合中。You add parameters to the WhereParameters collection when you want to programmatically set one or more of the conditions in the Where clause. 例如,您可以在数据库表中搜索姓氏等于控件值的记录 TextBox 。For example, you might search a database table for records with a last name equal to the value of a TextBox control. 在这种情况下,可将参数添加到 WhereParameters 文本框值的集合。In that case, you add a parameter to the WhereParameters collection for the text box value.
如果在 Where 子句中无需在运行时设置值,则无需使用该 WhereParameters 集合。If you do not have to set a value at run time in the Where clause, you do not have to use the WhereParameters collection. 可以在属性中定义要检索的字段 Where 。You can define the fields to retrieve in the Where property. 例如,若要从数据库表返回值,而该数据库表的 LastName 标记中等于 "Adams",则将 Where 属性设置为 'LastName = "Adams"' 不带任何参数。For example, to return values from a database table where LastName equals "Adams" in markup, set the Where property to 'LastName = "Adams"' without any parameters.
若要设置集合中的值 WhereParameters ,请为每个参数分配一个名称,然后在该参数的属性中添加占位符 Where 。To set values in the WhereParameters collection, you assign a name to each parameter and then add a placeholder in the Where property for that parameter. 在 Where 子句中,每个参数名称前面加上 @ 符号。In the Where clause, preface each parameter name with the @ symbol.