簡體   English   中英

復選框列表中只勾選了一個復選框

[英]Only one checkbox is checked in Checkbox list

我在 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;
                    }
                }
            }
    }

在 aspx 文件中,我通過代碼綁定了一個帶有名稱和值對的復選框

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

在瀏覽器中呈現頁面后,復選框列表將填充

  • 財產和傷亡 [ ]
  • 意外與健康 [ ]
  • 生活 [ ]
  • 意外 [ ]
  • 沒有任何 [ ]

如果字符串名稱包含諸如“財產和傷亡、事故和健康、生命”之類的值,則函數 FillAfterClose 僅​​啟用“財產和傷亡”復選框並省略其余部分...我想要“財產和傷亡、事故和健康、生命”應選中復選框。

如果字符串正好是:

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

然后我會看到拆分末尾的空格有問題。 我會改變這一行:

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

這將導致:

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

到這一行:

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

這將導致:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM