C# 检查是否至少选择了一个复选框列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15251522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if at least one checkboxlist is selected
提问by user1858332
I have checkboxlist and I would like to check if at least one checkbox is checked. If none is checked then I want to show alert message that says please select at least one item. I want to do this in code behind if possible. I have started but don't know if it is right or wrong but not able to finish it.
我有复选框列表,我想检查是否至少选中了一个复选框。如果没有选中,那么我想显示提示消息,说请至少选择一项。如果可能,我想在后面的代码中执行此操作。我已经开始了,但不知道是对还是错,但无法完成。
public void alert()
{
foreach (ListItem listItem in cblCustomerList.Items)
{
if (!listItem.Selected)
{
}
}
}
here is the checkboxlist in aspx:
这是aspx中的复选框列表:
<asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList"
DataTextField="GroupName" DataValueField="GroupName"
onclick="readCheckBoxList()" >
</asp:CheckBoxList>
here is the button:
这是按钮:
<asp:Button ID="Button1" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" OnClientClick="return Validate_Checkbox()" />
Thanks for your help.
谢谢你的帮助。
采纳答案by Soner G?nül
Edit:
编辑:
Here is one sample code. its working to me
这是一个示例代码。它对我有用
Must add this script file :<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
必须添加这个脚本文件:<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function Validate_Checkbox() {
var chks = $("#<%= cblCustomerList.ClientID %> input:checkbox");
var hasChecked = false;
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
hasChecked = true;
break;
}
}
if (hasChecked == false) {
alert("Please select at least one checkbox..!");
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="cblCustomerList" runat="server" CssClass="CheckBoxList">
<asp:ListItem Value="0">xx</asp:ListItem>
<asp:ListItem Value="1">yy</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="xx" runat="server" OnClientClick="javascript:Validate_Checkbox();return true;" />
</div>
</form>
</body>
</html>
and you change
你改变了
<asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList" DataTextField="GroupName" DataValueField="GroupName">
</asp:CheckBoxList>
control instead of my sample code . and call the javascript function
in the button control.look my sample code.
控制而不是我的示例代码。并调用javascript function
按钮控件中的 。看看我的示例代码。
Chears!!!
加油!!!
Edit
编辑
please add this script file
请添加此脚本文件
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Instead of <script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
代替 <script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
回答by sll
// using System.Linq;
// Considering that items are of ListItem type, otherwise use Cast<ListItem>()
if (!cblCustomerList.Items.Any(i => i.Selected))
{
// TODO: Warn an user
}
回答by Sergey Berezovskiy
if(cblCustomerList.Items.Cast<ListItem>().Any(item => item.Selected))
{
// at least one selected
}
回答by Soner G?nül
Try this;
尝试这个;
boolean b = cblCustomerList.Items.Cast<ListItem>().Any(i => i.Selected)
if b
is true
, at least one is selected in your checkboxlist.
如果b
是true
,则在您的复选框列表中至少选择了一个。
Don't forget to use System.Linq
namespace.
不要忘记使用System.Linq
命名空间。
回答by Greg B
You would just use cblCustomerList.SelectedItem == null
, SelectedItem will return the lowest ordinal item in the list of checked items, so if nothing is returned, nothing is checked.
您只需使用cblCustomerList.SelectedItem == null
, SelectedItem 将返回已检查项目列表中的最低序数项目,因此如果未返回任何内容,则不会检查任何内容。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
回答by SamuraiHyman
The simplest way i can think of....
我能想到的最简单的方法......
public void alert()
{
int i=0;
foreach (ListItem listItem in cblCustomerList.Items)
{
if (listItem.Selected)
{
i++;
}
}
if(i !=0)
{
Response.Write("Please check at least one option");
}
}
}
回答by Prasad Kanaparthi
if(! cblCustomerList.Items.Cast<ListItem>().AsParallel().Any(i => i.Selected))
{
ShowAlert("Please select at least one option");
}
回答by naveen
jQuery solution.
jQuery 解决方案。
if (!$(".CheckBoxList").find("input:checked").length) {
alert("Houston, we've had a problem!");
}