简体   繁体   English

复选框列表中只勾选了一个复选框

[英]Only one checkbox is checked in Checkbox list

I have written a function in asp.net c# application我在 asp.net c# 应用程序中编写了一个函数

     public void FillAfterClose(string Names)
    {

            string[] arrGroup = Names.ToString().Split(',');

            foreach (object obj in arrGroup)
            {

                for (int i = 0; i < chklLicenceTypes.Items.Count; i++)
                {
                    if (chklLicenceTypes.Items[i].Text == obj.ToString())
                    {
                        chklLicenceTypes.Items[i].Selected = true;
                    }
                }
            }
    }

in aspx file I am binding a checkbox with name and value pair via code在 aspx 文件中,我通过代码绑定了一个带有名称和值对的复选框

     <asp:CheckBoxList ID="chklLicenceTypes" RepeatDirection="Horizontal" AutoPostBack="false" 
CausesValidation="false" RepeatColumns="3" RepeatLayout="Table" runat="server">
    </asp:CheckBoxList>

After rendering the page in browser the checkboxlist are filled with在浏览器中呈现页面后,复选框列表将填充

  • Property and Casualty [ ]财产和伤亡 [ ]
  • Accident and Health [ ]意外与健康 [ ]
  • Life [ ]生活 [ ]
  • Casuality [ ]意外 [ ]
  • None [ ]没有任何 [ ]

If string Names contains value like "Property and Casualty, Accident and Health, Life" then Function FillAfterClose only makes "Property and Casualty" checkbox enabled and rest are omitted... I want that "Property and Casualty, Accident and Health, Life" checkboxes should be checked.如果字符串名称包含诸如“财产和伤亡、事故和健康、生命”之类的值,则函数 FillAfterClose 仅​​启用“财产和伤亡”复选框并省略其余部分...我想要“财产和伤亡、事故和健康、生命”应选中复选框。

If the string is exactly:如果字符串正好是:

var names="Property and Casualty, Accident and Health, Life";

Then I would see that there is a problem with the spaces in the end of the split.然后我会看到拆分末尾的空格有问题。 I would change this line:我会改变这一行:

string[] arrGroup = Names.ToString().Split(',');

this will result to:这将导致:

new []
    {
        "Property and Casualty",
        " Accident and Health",
        " Life"
    };

To this line:到这一行:

string[] arrGroup = names.Split(',').Select (s =>s.Trim()).ToArray();

this will result to:这将导致:

new []
    {
        "Property and Casualty",
        "Accident and Health",
        "Life"
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM