【问题标题】:asp.net C# check value of multiple DropDownListasp.net C#检查多个DropDownList的值
【发布时间】:2020-07-06 05:57:29
【问题描述】:

我做了一个搜索按钮,它将显示从 sql 到 gridview 的数据,搜索按钮与 DropDownList 连接,其中包含两个值(是)和(否),所以如果用户选择(是)它应该获取数据所在的位置value 等于 (yes) 与 (no) 相同。这对我来说很好,我确实分开吃一个,但如果我同时搜索两个 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>

【问题讨论】:

  • else if (DropDownList_pc.SelectedValue == "1") 在这一行为什么有一个“else if”而不是only if?
  • SqlDataSource1DropDownList1DropDownList2有什么关系?

标签: c# asp.net dropdown


【解决方案1】:

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

    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;

【讨论】:

  • 但我需要添加一些内容以使其完美运行,因为如果使用您的代码,如果我只选择 DropDownList_pc,它将给我错误,所以为了解决这个问题,我需要添加字符串命令值的位置,所以其余条件只能有 AND,
【解决方案2】:

看看后面代码的逻辑。您正在使用 if-elseif-elseif 等。这意味着满足的第一个条件将被触发,而任何进一步的条件子句都不会。因此,您希望将与单独的下拉菜单相关的 if 条件分成单独的 if 子句。

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

// separate dropdown, separate logic!
if (DropDownList_pc.SelectedValue == "1")
{
  SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_pc = 'yes'";
}
else if (DropDownList_pc.SelectedValue == "2")
{
  SqlDataSource1.SelectCommand = "SELECT * FROM emp_table where inv_pc = 'no'";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多