【问题标题】:Set Multiple default values to a SqlDataSource Where Condition将多个默认值设置为 SqlDataSource Where 条件
【发布时间】:2016-12-07 14:14:41
【问题描述】:

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

下拉列表

    <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>

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    您需要修改 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)
    

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多