首页 > 解决方案 > asp.net C#检查多个DropDownList的值

问题描述

我做了一个搜索按钮,它将显示从 sql 到 gridview 的数据,搜索按钮与 DropDownList 连接,其中包含两个值(是)和(否),所以如果用户选择(是)它应该得到值等于的数据(是)和(否)相同。这对我来说很好,我确实分开吃一个,但如果我同时搜索两个 DropDownList,它会给我第一个 DropDownList 正确但第二个错误,例如如果 DropDownList1 = "yes" 显示(是)值,如果 DropDownList2 = "no" 显示 (no) 值,它将返回 (yes) 他们两个,

这是我的代码:

 if (DropDownList_laptop.SelectedValue == "1")
    {
        // ... code here
   SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_lap = 'yes'";
    }
    else if (DropDownList_laptop.SelectedValue == "2")
    {
        // ... code here
   SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_lap = 'no'";
    }

    else if (DropDownList_pc.SelectedValue == "1")
    {
        // ... code here
   SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_pc = 'yes'";
    }
    else if (DropDownList_pc.SelectedValue == "2")
    {
        // ... code here
    SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_pc = 'no'";
    }

下拉代码:

<asp:DropDownList ID="DropDownList_laptop" runat="server" Height="29px" Width="70px">
                        <asp:listitem text="" value="0"></asp:listitem>
                        <asp:listitem text="yes" value="1"></asp:listitem>
                        <asp:listitem text="no" value="2"></asp:listitem>
                        </asp:DropDownList>

标签: c#asp.netdropdown

解决方案


我认为这样的事情应该适合你:

    string command = "SELECT * FROM emp_table";
    
    if (DropDownList_laptop.SelectedValue == "1"){
        command += " WHERE inv_lap = 'yes'";
    }
    else if (DropDownList_laptop.SelectedValue == "2")
    {
        command += " WHERE inv_lap = 'no'";
    }

    if (DropDownList_pc.SelectedValue == "1")
    {
        command += " AND inv_pc = 'yes'";
    }
    else if (DropDownList_pc.SelectedValue == "2")
    {
        command += " AND inv_pc = 'no'";
    }
    
    SqlDataSource1.SelectCommand = command;

推荐阅读