Reputation: 43
I have a series of Dropdown boxes that contain an option for Pass or Fail. What I want to happen is if any of the 6 dropdown boxes in the panel contain a Fail selection, Button 1 appears. If there are no Fail selections made, then Button 2 appears.
Here is what I have, this doesn't seem to get all the selectedvalues of the dropdowns, only the first selection. I call the same CheckDDL() method after each DropDown_SelectedIndexChanged event. Any help is greatly appreciated!
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();
}
ASPX page code:
<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>
Upvotes: 0
Views: 144
Reputation: 184
So first off you are creating a new DataTable
on each loop which means your code bool containsfail = dt.AsEnumerable().Any(row => fail == row.Field<String>("result"));
is only checking the most recent droplist!
But I suggest you try this
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;
you can fine tune the buttons to your liking.
Upvotes: 1
Reputation: 582
Use count
variable. If one of the dropdownlist contains fail
then increment count
to 1 and then check for 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;
}
}
Upvotes: 1