2013-03-06 181 views
6

我有复选框,我想检查是否至少有一个复选框被选中。如果没有选中,我想显示警报消息,说请至少选择一个项目。如果可能的话,我想在代码背后做这件事。我已经开始,但不知道它是对还是错,但无法完成。检查是否至少选择了一个复选框

public void alert() 
    { 
     foreach (ListItem listItem in cblCustomerList.Items) 
     { 
      if (!listItem.Selected) 
      { 
      } 
     } 
    } 

这里是在ASPX中的CheckBoxList:

<asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList" 
      DataTextField="GroupName" DataValueField="GroupName" 
       onclick="readCheckBoxList()" >    
      </asp:CheckBoxList> 

这里的按钮:

<asp:Button ID="Button1" runat="server" CausesValidation="True" 
          CommandName="Insert" Text="Insert" OnClientClick="return Validate_Checkbox()" /> 

感谢您的帮助。

+1

应该在JS中,而不是在代码背后 – 2013-03-06 15:37:49

+0

最好在客户端使用javascript进行这种检查。这里没有任何东西取决于来自服务器的数据 – codingbiz 2013-03-06 15:38:34

+0

@both绕过客户端验证确实非常简单。需要验证代码是完全合理的。 – BinaryTox1n 2013-03-06 15:39:30

回答

6

编辑:

这是一个示例代码。它的工作对我来说

必须添加这个脚本文件:<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> 

并更改

<asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList" DataTextField="GroupName" DataValueField="GroupName">    
      </asp:CheckBoxList> 

控制,而不是我的示例代码。并在button控件中调用javascript function。查看我的示例代码。

Chears !!!

编辑

请加这个脚本文件

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 

而不是<script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>

+0

美化你的js代码... – naveen 2013-03-06 15:51:14

+0

我的控件ID被称为cblCustomerList你在JavaScript中调用那个?这里是aspx中的checkboxlist: user1858332 2013-03-06 15:58:50

+0

我上面有新的编辑过的代码。请检查新代码。其工作 – 2013-03-06 16:04:33

3
// 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 
} 
9
if(cblCustomerList.Items.Cast<ListItem>().Any(item => item.Selected)) 
{ 
    // at least one selected 
} 
4

试试这个;

boolean b = cblCustomerList.Items.Cast<ListItem>().Any(i => i.Selected) 

如果btrue,至少在你的CheckBoxList选择。

不要忘记使用System.Linq命名空间。

0

我能想到的最简单的方法....

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"); 
      } 
     } 
    } 
1
 if(! cblCustomerList.Items.Cast<ListItem>().AsParallel().Any(i => i.Selected)) 
     { 
      ShowAlert("Please select at least one option"); 
     } 
+0

@naveen,感谢您的指出。我改变了逻辑。 – 2013-03-06 15:56:25

1

jQuery的解决方案。

if (!$(".CheckBoxList").find("input:checked").length) { 
    alert("Houston, we've had a problem!"); 
} 
相关问题