问题描述
我有一系列下拉框,其中包含“通过”或“失败”选项。我要发生的是,如果面板中的6个下拉框中的任何一个包含“失败”选择,就会出现按钮1。如果没有选择失败,则出现按钮2。
这就是我所拥有的,这似乎并没有获得下拉菜单的所有selectedvalues,只有第一个选择。在每个DropDown_SelectedindexChanged事件之后,我都调用相同的CheckDDL()方法。任何帮助将不胜感激!
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();
}
<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>
解决方法
使用count
变量。如果下拉列表之一包含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;
}
}
,
因此,首先,您需要在每个循环上创建一个新的DataTable
,这意味着您的代码bool containsfail = dt.AsEnumerable().Any(row => fail == row.Field<String>("result"));
仅检查最新的Droplist!
但是我建议您尝试一下
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;
您可以根据自己的喜好微调按钮。