2013-03-06 595 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!"); 
} 
相關問題