【问题标题】:Check if at least one checkboxlist is selected检查是否至少选择了一个复选框列表
【发布时间】:2013-02-21 11:46:10
【问题描述】:

我有复选框列表,我想检查是否至少选中了一个复选框。如果没有选中,那么我想显示警告消息,说请至少选择一项。如果可能的话,我想在后面的代码中做到这一点。我已经开始了,但不知道是对还是错,但无法完成。

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

这是 aspx 中的复选框列表:

 <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()" />

感谢您的帮助。

【问题讨论】:

  • 应该在JS中,而不是在后面的代码中
  • 最好使用javascript在客户端进行这种检查。这里没有什么依赖于来自服务器的数据
  • @both 绕过客户端验证真的很容易。想要验证代码是完全合理的。
  • 警报部分是我担心的不是检查。检查已经在答案中。可能会在页面上放置一个标签并将 text 属性设置为您的错误消息。 &lt;asp:Label ForeColor="Red" /&gt;
  • @codingbiz +1 用于阅读理解。我什至没有注意到这一点。提问者:您是在尝试客户端还是服务器端验证?

标签: c# asp.net checkboxlist


【解决方案1】:
if(cblCustomerList.Items.Cast<ListItem>().Any(item => item.Selected))
{
   // at least one selected
}

【讨论】:

    【解决方案2】:

    编辑:

    这是一个示例代码。它对我有用

    必须添加这个脚本文件:&lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;

     <%@ 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>
    

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

    切!!!

    编辑

    请添加此脚本文件

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

    而不是&lt;script src="scripts/jquery-1.8.3.min.js" type="text/javascript"&gt;&lt;/script&gt;

    【讨论】:

    • 我的控件 ID 被称为 cblCustomerList 你在哪里调用它在 javascript?这是我在 aspx 中的复选框列表:
    • 我上面有新的编辑代码。请检查新代码。它的工作
    • 谢谢,但即使我选择了一项,它也会显示警报。我不知道发生了什么请帮忙谢谢
    • 嘿,我的新编辑代码在我的本地运行良好。您只需更改复选框控件。
    • 我收到不同的错误,如下所示:“错误:对象不支持此属性或方法”。 OnClientClick 不绑定到复选框列表,但它绑定到按钮。请。请参阅上面的更新代码。谢谢
    【解决方案3】:

    试试这个;

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

    如果btrue,则在您的复选框列表中至少选择了一个。

    别忘了使用System.Linq 命名空间。

    【讨论】:

      【解决方案4】:
      // 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
      }
      

      【讨论】:

        【解决方案5】:

        您只需使用cblCustomerList.SelectedItem == null,SelectedItem 将返回已检查项目列表中最低的序号项目,因此如果未返回任何内容,则不检查任何内容。

        http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx

        【讨论】:

          【解决方案6】:
                  if(! cblCustomerList.Items.Cast<ListItem>().AsParallel().Any(i => i.Selected))
                  {
                     ShowAlert("Please select at least one option");
                  }
          

          【讨论】:

            【解决方案7】:

            jQuery 解决方案。

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

            【讨论】:

              【解决方案8】:

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

              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");
                          }
                      }
                  }
              

              【讨论】:

                【解决方案9】:

                使用内置的“CheckedItems”:

                 dim x1 As Integer = clbMoveTo.CheckedItems.Count
                 If x1 <= 0 Then
                     MessageBox.Show("Select Move To.","Verify")
                     return
                  End If
                

                【讨论】:

                  猜你喜欢
                  • 2012-04-11
                  • 1970-01-01
                  • 2015-06-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多