有什么简单的方法来检查复选框列表控件中是否没有选择任何项目?

发布于 2024-12-02 09:15:59 字数 7 浏览 0 评论 0原文

I know I can loop through a checkboxlist.Items and see if none of them is selected, but is there a better way to find out if no items have been selected, in the code behind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海夜未眠 2024-12-09 09:15:59

您必须检查 SelectedIndex。 If it equals -1, its means nothing is selected.

 CheckBoxList list = new CheckBoxList();
 if (list.SelectedIndex == -1)
 {
      //Nothing is selected
 }

You have to check the SelectedIndex. If it equals -1, its means nothing is selected.

 CheckBoxList list = new CheckBoxList();
 if (list.SelectedIndex == -1)
 {
      //Nothing is selected
 }
扶醉桌前 2024-12-09 09:15:59

CheckBoxList 具有 属性SelectedIndexSelectedValue

可以检查是否有SelectedIndex;如果没有检查任何项目,它将是-1。

CheckBoxList has properties for SelectedIndex and SelectedValue.

You can check whether there is a SelectedIndex; it will be -1 if no items are checked.

旧城空念 2024-12-09 09:15:59

这可能是验证 CheckBoxList 的最简单方法:

使用自定义验证器:

<asp:CustomValidator runat="server" ID="cvmodulelist" ClientValidationFunction="ValidateModuleList" ErrorMessage="Please Select Atleast one Module"></asp:CustomValidator>

创建 JavaScript 函数来验证 CheckBoxList:

// javascript to add to your aspx page
function ValidateModuleList(source, args)
{
  var chkListModules= document.getElementById ('<%= chkModuleList.ClientID %>');
  var chkListinputs = chkListModules.getElementsByTagName("input");
  for (var i=0;i<chkListinputs .length;i++)
  {
    if (chkListinputs [i].checked)
    {
      args.IsValid = true;
      return;
    }
  }
  args.IsValid = false;
}

另一种选择是创建自定义验证控件,如下所示:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomValidators
{
  public class RequiredFieldValidatorForCheckBoxLists : System.Web.UI.WebControls.BaseValidator 
  {
     private ListControl _listctrl;

     public RequiredFieldValidatorForCheckBoxLists()
     {
       base.EnableClientScript = false;
     }

     protected override bool ControlPropertiesValid()
     {
       Control ctrl = FindControl(ControlToValidate);

       if (ctrl != null) 
       {
         _listctrl = (ListControl) ctrl;
         return (_listctrl != null);   
       }
       else 
         return false;  // raise exception
     }

     protected override bool EvaluateIsValid()
     {     
       return _listctrl.SelectedIndex != -1;
     }
   }
}

This is probably the easiest way to validate the CheckBoxList:

Use a custom validator:

<asp:CustomValidator runat="server" ID="cvmodulelist" ClientValidationFunction="ValidateModuleList" ErrorMessage="Please Select Atleast one Module"></asp:CustomValidator>

Create a JavaScript function to validate the CheckBoxList:

// javascript to add to your aspx page
function ValidateModuleList(source, args)
{
  var chkListModules= document.getElementById ('<%= chkModuleList.ClientID %>');
  var chkListinputs = chkListModules.getElementsByTagName("input");
  for (var i=0;i<chkListinputs .length;i++)
  {
    if (chkListinputs [i].checked)
    {
      args.IsValid = true;
      return;
    }
  }
  args.IsValid = false;
}

Another option is to create a custom validation control, like this:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomValidators
{
  public class RequiredFieldValidatorForCheckBoxLists : System.Web.UI.WebControls.BaseValidator 
  {
     private ListControl _listctrl;

     public RequiredFieldValidatorForCheckBoxLists()
     {
       base.EnableClientScript = false;
     }

     protected override bool ControlPropertiesValid()
     {
       Control ctrl = FindControl(ControlToValidate);

       if (ctrl != null) 
       {
         _listctrl = (ListControl) ctrl;
         return (_listctrl != null);   
       }
       else 
         return false;  // raise exception
     }

     protected override bool EvaluateIsValid()
     {     
       return _listctrl.SelectedIndex != -1;
     }
   }
}
别忘他 2024-12-09 09:15:59

您必须检查所选索引。如果所选索引为-1,则表示没有选择任何项目。

if (CheckBoxList1.SelectedIndex == -1)
{
     Response.Write("<script> alert('Show your message') </script>");
}

You have to check selected index. If selected index is -1 it means that no item is being selected.

if (CheckBoxList1.SelectedIndex == -1)
{
     Response.Write("<script> alert('Show your message') </script>");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文