我在asp.net c#application
中编写了一个函数 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>
在浏览器中渲染页面后,复选框列表中填充了
如果字符串名称包含“Property and Casualty,Accident and Health,Life”之类的值,则Function FillAfterClose仅启用“Property and Casualty”复选框并忽略其余... 我想要检查“财产和伤亡,意外和健康,生活”复选框。
答案 0 :(得分:1)
如果字符串完全是:
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"
};