将多个默认值设置为SqlDataSource Where Condition

时间:2016-12-07 14:14:42

标签: c# asp.net webforms

我有一个从SqlDataSource填充的列表视图。它根据查询字符串中的类别值显示数据库中的产品列表。我在产品表中的类别是New|Fiction|Auto|Text。如何设置默认值以显示所有类别。从DropdownList中选择类别。当我在下拉列表中选择ALL时,我需要显示所有产品。

DROPDOWNLIST

    <asp:DropDownList runat="server" id="ddl" OnSelectedIndexChanged="SelectionChanged" AutoPostBack="true">
         <asp:ListItem Text="All" ></asp:ListItem>
        <asp:ListItem Text="Fiction" Value="~/Bio.aspx?Category=Fiction" />
        <asp:ListItem Text="TextBooks" Value="~/Bio.aspx?Category=Text" />
        <asp:ListItem Text="Biography" Value="~/Bio.aspx?Category=Auto" />
         <asp:ListItem Text="New Release" Value="~/Bio.aspx?Category=New" />
    </asp:DropDownList>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:2016_675_z1787626ConnectionString %>" SelectCommand="SELECT * FROM [Bio] WHERE ([Category] = @Category)" >
        <SelectParameters>
            <asp:QueryStringParameter Name="Category" QueryStringField="Category" Type="String" />
        </SelectParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

您需要修改select语句中的where子句。

<asp:DropDownList runat="server" id="ddl" 
   OnSelectedIndexChanged="SelectionChanged" 
   AutoPostBack="true">
   <asp:ListItem Text="All" Value="~/Bio.aspx?Category=ALL" ></asp:ListItem>
   ...
</asp:DropDownList>

SELECT * FROM [Bio] WHERE (@Category = '' OR @Category = 'ALL' OR [Category] = @Category)