C# 检查面板中的dropdownlist.selectedvalue是否包含特定值
我有一系列下拉框,其中包含通过或失败选项。我想发生的是,如果面板中的6个下拉框中的任何一个包含失败选择,则会出现按钮1。如果未进行失败选择,则出现按钮2 这就是我所拥有的,它似乎没有得到下拉列表中所有的SelectedValue,只有第一个选择。在每个下拉菜单\u SelectedIndexChanged事件之后,我调用相同的CheckDDL()方法。非常感谢您的帮助C# 检查面板中的dropdownlist.selectedvalue是否包含特定值,c#,asp.net,dropdown,C#,Asp.net,Dropdown,我有一系列下拉框,其中包含通过或失败选项。我想发生的是,如果面板中的6个下拉框中的任何一个包含失败选择,则会出现按钮1。如果未进行失败选择,则出现按钮2 这就是我所拥有的,它似乎没有得到下拉列表中所有的SelectedValue,只有第一个选择。在每个下拉菜单\u SelectedIndexChanged事件之后,我调用相同的CheckDDL()方法。非常感谢您的帮助 public void CheckDDL() { var myDropDownLists
public void CheckDDL()
{
var myDropDownLists = TestResults.Controls
.OfType<DropDownList>()
.Where(ddl => ddl.Enabled = true);
foreach (DropDownList ddl in myDropDownLists)
{
DataTable dt = new DataTable();
dt.Columns.Add("result");
string result = ddl.SelectedValue;
DataRow dr = dt.NewRow();
dr["result"] = result;
string fail = "0";
bool containsfail = dt.AsEnumerable().Any(row => fail == row.Field<String>("result"));
if (containsfail == true)
{
btnAllPass.Visible = false;
btnAddredo.Visible = true;
}
else
{
btnAllPass.Visible = true;
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckDDL();
}
public void CheckDDL()
{
var myDropDownLists=TestResults.Controls
第()类
.Where(ddl=>ddl.Enabled=true);
foreach(MyDropDownList中的DropDownList ddl)
{
DataTable dt=新的DataTable();
dt.列。添加(“结果”);
字符串结果=ddl.SelectedValue;
DataRow dr=dt.NewRow();
dr[“结果”]=结果;
字符串fail=“0”;
bool containsfail=dt.AsEnumerable().Any(row=>fail==row.Field(“结果”);
if(containsfail==true)
{
btnAllPass.Visible=false;
btnAddredo.Visible=true;
}
其他的
{
btnAllPass.Visible=true;
}
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
CheckDDL();
}
ASPX页面代码:
<asp:Panel ID="TestResults" runat="server" Width="1040px" BorderStyle="Double" BorderWidth="2px" Height="280px">
<br />
<table id="HBredotable" style="width: 625px">
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label2" runat="server" Text="Serial One:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox1" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--" Value="2"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label3" runat="server" Text="Serial Two:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox2" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label4" runat="server" Text="Serial Three:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox3" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label5" runat="server" Text="Serial Four:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox4" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList4" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label6" runat="server" Text="Serial Five:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox5" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList5" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList5_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 189px" align="center">
<asp:Label ID="Label7" runat="server" Text="Serial Six:"></asp:Label>
</td>
<td style="width: 248px">
<asp:TextBox ID="TextBox6" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="DropDownList6" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList6_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
<asp:ListItem Text="--Select--"></asp:ListItem>
<asp:ListItem Text="Pass" Value="1"></asp:ListItem>
<asp:ListItem Text="Fail" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:Label ID="lblREDOerror" runat="server" Text="" ForeColor="red"></asp:Label><br />
<asp:Button ID="btnAllPass" runat="server" Text="All Tests Pass / Unit Complete" Width="210px" OnClick="btnAllPass_Click"/>
<asp:Button ID="btnAddredo" runat="server" Text="Enter Hashboards for REDO" Width="210px" OnClick="btnAddredo_Click" />
<asp:Button ID="btnconfirmredo" runat="server" Text="Yes, Continue" Width="210px" OnClick="btnconfirmredo_Click" />
<asp:Button ID="btnreturn" runat="server" Text="No, Go Back" Width="210px" OnClick="btnreturn_Click" />
<br />
</asp:Panel>
使用计数
变量。如果其中一个下拉列表包含fail
,则将count
增加到1,然后检查count
public void CheckDDL()
{
var myDropDownLists = TestResults.Controls
.OfType<DropDownList>()
.Where(ddl => ddl.Enabled = true);
int count=0;
foreach (DropDownList ddl in myDropDownLists)
{
string result = ddl.SelectedValue;
if(result=="fail")
{
count++;
break;
}
else
{
continue;
}
}
if (count > 0)
{
btnAllPass.Visible = false;
btnAddredo.Visible = true;
}
else
{
btnAllPass.Visible = true;
}
}
public void CheckDDL()
{
var myDropDownLists=TestResults.Controls
第()类
.Where(ddl=>ddl.Enabled=true);
整数计数=0;
foreach(MyDropDownList中的DropDownList ddl)
{
字符串结果=ddl.SelectedValue;
如果(结果=“失败”)
{
计数++;
打破
}
其他的
{
继续;
}
}
如果(计数>0)
{
btnAllPass.Visible=false;
btnAddredo.Visible=true;
}
其他的
{
btnAllPass.Visible=true;
}
}
使用计数
变量。如果其中一个下拉列表包含fail
,则将count
增加到1,然后检查count
public void CheckDDL()
{
var myDropDownLists = TestResults.Controls
.OfType<DropDownList>()
.Where(ddl => ddl.Enabled = true);
int count=0;
foreach (DropDownList ddl in myDropDownLists)
{
string result = ddl.SelectedValue;
if(result=="fail")
{
count++;
break;
}
else
{
continue;
}
}
if (count > 0)
{
btnAllPass.Visible = false;
btnAddredo.Visible = true;
}
else
{
btnAllPass.Visible = true;
}
}
public void CheckDDL()
{
var myDropDownLists=TestResults.Controls
第()类
.Where(ddl=>ddl.Enabled=true);
整数计数=0;
foreach(MyDropDownList中的DropDownList ddl)
{
字符串结果=ddl.SelectedValue;
如果(结果=“失败”)
{
计数++;
打破
}
其他的
{
继续;
}
}
如果(计数>0)
{
btnAllPass.Visible=false;
btnAddredo.Visible=true;
}
其他的
{
btnAllPass.Visible=true;
}
}
因此,首先在每个循环上创建一个新的数据表,这意味着您的代码bool containsfail=dt.AsEnumerable().Any(row=>fail==row.Field(“结果”)代码>仅检查最近的液滴列表
但我建议你试试这个
var myDropDownLists = TestResults.Controls
.OfType<DropDownList>()
.Where(ddl => ddl.Enabled = true);
var anyFail = myDropDownLists.Any(dd => dd.SelectedValue == "0");
btnAllPass.Visible = !anyFail;
btnAddredo.Visible = anyFail;
var myDropDownLists=TestResults.Controls
第()类
.Where(ddl=>ddl.Enabled=true);
var anyFail=myDropDownLists.Any(dd=>dd.SelectedValue==“0”);
btnAllPass.Visible=!任何失败;
btnAddredo.Visible=anyFail;
<