LinqDataSource.AutoGenerateWhereClause 屬性

定義

取得或設定值,這個值表示 LinqDataSource 控制項是否根據 WhereParameters 集合中所定義的值動態地建立 Where 子句。

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

屬性值

Boolean

如果 LinqDataSource 控制項要建立 Where 子句則為 true,否則為 false。 預設為 false

實作

範例

下列範例顯示 LinqDataSource 控制項,其設定為 AutoGenerateWhereClause true 。 控制項 GridView 系結至 LinqDataSource 控制項,以顯示從查詢傳回的資料。 包含 DropDownList 的控制項會填入三個值。 參數包含在集合中 WhereParameters ,其名稱設定為 Category, ,其符合其中一個資料物件的屬性。 其 ControlID 屬性會設定為 控制項的 DropDownList 識別碼。 控制項 LinqDataSource 會自動建立 屬性, Where 根據使用者從 DropDownList 控制項中選取的值來篩選記錄。 查詢會傳回其 屬性符合使用者從 控制項選取的值的 DropDownList 記錄 Category

<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 控制項會動態地從集合中的 WhereParameters 參數建立 Where 子句。 您新增至 WhereParameters 集合的每個參數都必須將其 Name 屬性設定為符合所查詢資料物件中屬性的值。 自動產生的 Where 子句會檢查集合中指定的 WhereParameters 值是否等於資料物件中相符屬性的值。 如果您提供多個參數,則參數會與邏輯 AND 作業連結。 包含 null 或空白值的參數不會包含在 Where 子句中。

自動產生的 Where 子句只能測試是否相等,而且只能與 AND 作業連結參數。 如果您必須新增不測試是否相等的條件,或必須與 OR 作業建立關聯的參數,請勿將 AutoGenerateWhereClause 屬性 true 設定為 。 您可以將 屬性設定 AutoGenerateWhereClausefalse ,並在 集合中每個參數的 屬性中 Where WhereParameters 新增預留位置,來完成這些工作。 在 屬性中 Where ,在每個預留位置名稱前面加上 @ 符號。

當 屬性為 trueAutoGenerateWhereClause ,您不會設定 Where 屬性,因為剖析器會動態建立 Where 子句。 如果 AutoGenerateWhereClause 屬性為 true ,且已指派 屬性, Where 控制項 LinqDataSource 會擲回例外狀況。

適用於