c# - 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>
解决方案
我认为这样的事情应该适合你:
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;
推荐阅读
- python - 在Python中查找所有满足子字典参数的字典
- java - toString() 方法,String.format() 方法。如何调整字符串线
- python - 如何导航到主页?- 蟒蛇django
- java - 通过 ADB 导入 .VCF 文件
- vue.js - 无法访问已安装的道具
- selenium-webdriver - 如何将 Codeception 集成到本地代码中?
- python - 如何通过 Python 脚本将列添加到 Athena 存在表中的特定位置?
- python - 填充字典时出现 KeyError
- forms - 网站定制 联系我们 表单定制
- android - 如何从我上传到firebase存储的文件中获取文件