如何检查是否至少选中了一个复选框?
How to check if at least one checkbox is checked?
我试图仅在每个复选框列表中至少选中一个复选框时才将我的数据绑定到 gridview。但是它似乎不起作用,因为当我单击提交时没有选中复选框它仍然进入绑定语句并且没有在标签中显示文本消息。
我的代码哪里出错了?请帮忙
if (IsPostBack)
{
if (CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
{
Bind();
}
else if (CheckBoxList1.SelectedValue == String.Empty)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (CheckBoxList2.SelectedValue == String.Empty)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
使用 linq Any
if (IsPostBack)
{
bool seleceted1 = CheckBoxList1.Items.Cast<ListItem>().Any(li => li.Selected);
bool seleceted2 = CheckBoxList2.Items.Cast<ListItem>().Any(li => li.Selected);
if (selected1 && selected2)
{
Bind();
}
else if (!selected1)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (!selected2)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
如果您想要至少一项检查项目,请使用 ||
运算符。不管来自哪个列表。
if (selected1 || selected2) // true if at least 1 item is checked
{
Bind();
}
您可以使用 Count 属性 来确定是否从 ComboBoxList 中选择了任何项目。计数将 return 选择的项目数,如果您没有标记任何选择,那么此 属性 将 return 0。
if (IsPostBack)
{
if (CheckBoxList1.Items.Cast<ListItem>().Count(li => li.Selected) != 0 &&
CheckBoxList2.Items.Cast<ListItem>().Count(i => i.Selected) != 0)
{
Bind();
}
else if (!CheckBoxList1.Checked)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (!CheckBoxList2.Checked)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
}
复选框的值不会为空,因此您只需要检查值是否为空,如下所示:
if (!string.IsNullOrEmpty( CheckBoxList1.SelectedValue) && !string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
{
Bind();
}
else
{
if (string.IsNullOrEmpty( CheckBoxList1.SelectedValue))
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
}
我认为If(IsPostback)
是罪魁祸首。如果您的页面已通过按钮 (PostBack) 刷新,那么您的复选框列表将 Bind()。因此,每次您单击页面中任意位置的按钮时,您的列表都会刷新,从而删除您选择的框。
尝试将 If(IsPostBack)
更改为 If(!IsPostBack)
编辑:
哦,知道了,您的 .SelectedValue 是一个字符串,因此它永远不会为 null。
改变这个
if(CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
至此
if(CheckBoxList1.SelectedValue != String.Empty && CheckBoxList2.SelectedValue != String.Empty)
并将 If(!IsPostBack)
还原为 If(IsPostBack)
,因为看起来此代码事件在 button_click
或其他东西下,而不是我假设的 PageLoad
。
如有疑虑,请联系我们。谢谢
简单的
for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++)
{
if (RadioButtonList1.Items[i].Selected==true)
{
//Bind here
}
}
我试图仅在每个复选框列表中至少选中一个复选框时才将我的数据绑定到 gridview。但是它似乎不起作用,因为当我单击提交时没有选中复选框它仍然进入绑定语句并且没有在标签中显示文本消息。
我的代码哪里出错了?请帮忙
if (IsPostBack)
{
if (CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
{
Bind();
}
else if (CheckBoxList1.SelectedValue == String.Empty)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (CheckBoxList2.SelectedValue == String.Empty)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
使用 linq Any
if (IsPostBack)
{
bool seleceted1 = CheckBoxList1.Items.Cast<ListItem>().Any(li => li.Selected);
bool seleceted2 = CheckBoxList2.Items.Cast<ListItem>().Any(li => li.Selected);
if (selected1 && selected2)
{
Bind();
}
else if (!selected1)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (!selected2)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
如果您想要至少一项检查项目,请使用 ||
运算符。不管来自哪个列表。
if (selected1 || selected2) // true if at least 1 item is checked
{
Bind();
}
您可以使用 Count 属性 来确定是否从 ComboBoxList 中选择了任何项目。计数将 return 选择的项目数,如果您没有标记任何选择,那么此 属性 将 return 0。
if (IsPostBack)
{
if (CheckBoxList1.Items.Cast<ListItem>().Count(li => li.Selected) != 0 &&
CheckBoxList2.Items.Cast<ListItem>().Count(i => i.Selected) != 0)
{
Bind();
}
else if (!CheckBoxList1.Checked)
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (!CheckBoxList2.Checked)
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
}
复选框的值不会为空,因此您只需要检查值是否为空,如下所示:
if (!string.IsNullOrEmpty( CheckBoxList1.SelectedValue) && !string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
{
Bind();
}
else
{
if (string.IsNullOrEmpty( CheckBoxList1.SelectedValue))
{
LABEL1.Text = ("Please select at least one checkbox();
}
else if (string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
{
LABEL2.Text = ("Please select at least one checkbox").ToString();
}
}
我认为If(IsPostback)
是罪魁祸首。如果您的页面已通过按钮 (PostBack) 刷新,那么您的复选框列表将 Bind()。因此,每次您单击页面中任意位置的按钮时,您的列表都会刷新,从而删除您选择的框。
尝试将 If(IsPostBack)
更改为 If(!IsPostBack)
编辑:
哦,知道了,您的 .SelectedValue 是一个字符串,因此它永远不会为 null。
改变这个
if(CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
至此
if(CheckBoxList1.SelectedValue != String.Empty && CheckBoxList2.SelectedValue != String.Empty)
并将 If(!IsPostBack)
还原为 If(IsPostBack)
,因为看起来此代码事件在 button_click
或其他东西下,而不是我假设的 PageLoad
。
如有疑虑,请联系我们。谢谢
简单的
for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++)
{
if (RadioButtonList1.Items[i].Selected==true)
{
//Bind here
}
}