如何在控制参数的同时搜索整个linqdatasource

时间:2014-01-23 08:59:58

标签: c# asp.net linq

有两个linqdatasource,一个绑定到下拉列表,另一个绑定到gridview。 DropDownList是控制参数。 我的代码:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="TestPortalDataContext" EntityTypeName="" EnableUpdate="true" EnableDelete="True" 
    EnableInsert="True" TableName="AllSuitesWithFeatureNames" 
    Where="FeatureName==@FeatureName" OrderBy="SuiteID desc" OnSelecting="LinqDataSource1_Selecting">
    <WhereParameters>
     <asp:QueryStringParameter DefaultValue="" Name="SuiteID" QueryStringField="SuiteID" />  
        <asp:ControlParameter ControlID="DropDownList1" DefaultValue="1" 
            Name="FeatureName" PropertyName="SelectedValue" Type="string" />
    </WhereParameters>
</asp:LinqDataSource>

<asp:LinqDataSource ID="LinqDataSource2" runat="server" 
    ContextTypeName="TestPortalDataContext" EntityTypeName="" 
    TableName="AllFeatures" OrderBy="FeatureName">
</asp:LinqDataSource>

<asp:DropDownList ID="DropDownList1" runat="server" CssClass="DDDropDown" 
    DataSourceID="LinqDataSource2" DataTextField="FeatureName" DataValueField="FeatureName" AutoPostBack="True" >
</asp:DropDownList>

由于LinqDataSource1中有一个控制参数,因此每次我必须在下拉列表中选择正确的FeatureName进行搜索。我知道在sql中很容易在所有表格中搜索。但是我怎样才能搜索所有LinqDataSource1 linq方式?

1 个答案:

答案 0 :(得分:0)

试试这个。假设

<asp:DropDownList ID="DropDownList1" runat="server" CssClass="DDDropDown" 
DataSourceID="LinqDataSource2" DataTextField="FeatureName" DataValueField="FeatureName" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        LinqDataSource1.Where = "FeatureName==" + DropDownList1.SelectedValue;
        LinqDataSource1.DataBind();
        gridView.DataSource = LinqDataSource1;

        gridView.DataBind();
    }