检查面板中是否有 dropdownlist.selectedvalue 包含特定值
Check if any dropdownlist.selectedvalue in panel contains a certain value
我有一系列下拉框,其中包含“通过”或“失败”选项。我想要发生的是,如果面板中的 6 个下拉框中的任何一个包含失败选择,按钮 1 就会出现。如果没有做出失败选择,则会出现按钮 2。
这是我所拥有的,这似乎没有得到下拉列表的所有选定值,只有第一个选择。我在每个 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();
}
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>
使用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"));
仅检查最新的下拉列表!
但我建议你试试这个
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;
您可以根据自己的喜好微调按钮。
我有一系列下拉框,其中包含“通过”或“失败”选项。我想要发生的是,如果面板中的 6 个下拉框中的任何一个包含失败选择,按钮 1 就会出现。如果没有做出失败选择,则会出现按钮 2。
这是我所拥有的,这似乎没有得到下拉列表的所有选定值,只有第一个选择。我在每个 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();
}
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>
使用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"));
仅检查最新的下拉列表!
但我建议你试试这个
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;
您可以根据自己的喜好微调按钮。