LinqDataSource.AutoGenerateWhereClause 属性
定义
获取或设置一个值,该值指示 LinqDataSource 控件是否基于 WhereParameters 集合中定义的值来动态创建 Where 子句。Gets or sets a value that indicates whether the LinqDataSource control dynamically creates a Where clause based on values defined in the WhereParameters collection.
public:
property bool AutoGenerateWhereClause { bool get(); void set(bool value); };
public bool AutoGenerateWhereClause { get; set; }
member this.AutoGenerateWhereClause : bool with get, set
Public Property AutoGenerateWhereClause As Boolean
属性值
如果 LinqDataSource 控件将创建 Where 子句,则为 true;否则为 false。true if the LinqDataSource control will create the Where clause; otherwise, false. 默认值为 false。The default is false.
实现
示例
下面的示例演示了一个 LinqDataSource 控件,该控件的 AutoGenerateWhereClause 设置为 true 。The following example shows a LinqDataSource control with the AutoGenerateWhereClause set to true. GridView控件绑定到 LinqDataSource 控件以显示从查询返回的数据。A GridView control is bound to the LinqDataSource control to display the data that is returned from the query. DropDownList包含用三个值填充的控件。A DropDownList control is included that is populated with three values. 参数包含在集合中,其 WhereParameters 名称设置为,与 Category, 数据对象的一个属性匹配。A parameter is included in the WhereParameters collection with the name set to Category, which matches one of the properties of the data object. ControlID将其属性设置为控件的 ID DropDownList 。Its ControlID property is set to the ID of the DropDownList control. LinqDataSource控件自动创建 Where 属性,以根据用户从控件中选择的值来筛选记录 DropDownList 。The LinqDataSource control automatically creates the Where property to filter records based on the value that the user selects from the DropDownList control. 查询返回的记录的 Category 属性与用户从控件中选择的值匹配 DropDownList 。The query returns the records whose Category property matches the value that the user has selected from the DropDownList control.
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
<asp:ListItem Value="Sports"></asp:ListItem>
<asp:ListItem Value="Garden"></asp:ListItem>
<asp:ListItem Value="Auto"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
AutoGenerateWhereClause="true"
ID="LinqDataSource1"
runat="server">
<WhereParameters>
<asp:ControlParameter
Name="Category"
ControlID="DropDownList1"
Type="String" />
</WhereParameters>
</asp:LinqDataSource>
<asp:GridView
DataSourceID="LinqDataSource1"
ID="GridView1"
runat="server">
</asp:GridView>
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
<asp:ListItem Value="Sports"></asp:ListItem>
<asp:ListItem Value="Garden"></asp:ListItem>
<asp:ListItem Value="Auto"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
AutoGenerateWhereClause="true"
ID="LinqDataSource1"
runat="server">
<WhereParameters>
<asp:ControlParameter
Name="Category"
ControlID="DropDownList1"
Type="String" />
</WhereParameters>
</asp:LinqDataSource>
<asp:GridView
DataSourceID="LinqDataSource1"
ID="GridView1"
runat="server">
</asp:GridView>
注解
将 AutoGenerateWhereClause 属性设置为时 true ,控件将 LinqDataSource 从集合中的参数动态创建 Where 子句 WhereParameters 。When you set the AutoGenerateWhereClause property to true, the LinqDataSource control dynamically creates the Where clause from the parameters in the WhereParameters collection. 添加到集合中的每个参数都 WhereParameters 必须 Name 将其属性设置为与正在查询的数据对象中的属性匹配的值。Each parameter that you add to the WhereParameters collection must have its Name property set to a value that matches a property in the data object that is being queried. 自动生成的 Where 子句将检查在集合中指定的值是否 WhereParameters 等于数据对象中的匹配属性的值。The automatically generated Where clause will check whether the value specified in the WhereParameters collection equals the value of the matching property in the data object. 如果提供多个参数,则这些参数将与逻辑 AND 操作链接。If you provide more than one parameter, the parameters are linked with a logical AND operation. 包含 null 或空值的参数不包含在 Where 子句中。Parameters that contain null or an empty value are not included in the Where clause.
自动生成的 Where 子句只能测试相等性,只能链接到操作的参数 AND 。The automatically generated Where clause can test only for equality and can link parameters only with the AND operation. AutoGenerateWhereClause true 如果必须添加不测试相等性的条件,或者必须将参数与操作相关联,请不要将属性设置为 OR 。Do not set the AutoGenerateWhereClause property to true if you have to add a condition that does not test for equality or if you have to relate parameters with the OR operation. 可以通过将 AutoGenerateWhereClause 属性设置为 false ,并在该集合中的 Where 每个参数的属性中添加占位符,来完成这些任务 WhereParameters 。You can accomplish these tasks by setting the AutoGenerateWhereClause property to false and adding placeholders in the Where property for each parameter in the WhereParameters collection. 在 Where 属性中,在每个占位符名称前面加上 @ 符号。In the Where property, preface each placeholder name with the @ symbol.
Where当属性为时,不会设置属性 AutoGenerateWhereClause true ,因为分析器会动态创建 Where 子句。You do not set the Where property when the AutoGenerateWhereClause property is true, because the parser dynamically creates the Where clause. LinqDataSource如果该属性为,则该控件引发异常 AutoGenerateWhereClause true ,并且为 Where 属性赋值。The LinqDataSource control throws an exception if the AutoGenerateWhereClause property is true and the Where property is assigned values.