简体   繁体   English

Checkboxlist C#-需要通过传递参数来检查内部项目

[英]Checkboxlist C# - Need to check items inside, by passing parameters

I have a CheckBoxList and check boxes with labels not fix, it would change. 我有一个CheckBoxList和带有标签的复选框不固定,它将改变。

I would like to pass multiple Strings in a method, which will read string and match with label names of checkboxes inside list, then check if matches. 我想在一个方法中传递多个字符串,该方法将读取字符串并与列表中复选框的标签名称匹配,然后检查是否匹配。

eg List contain following check boxes. 例如,列表包含以下复选框。

Chk1
Chk3
Chk5
Chk7

Method: Public void Method(str Stringstobeentered) 方法:公共无效方法(str字符串已输入)

User can pass (chk1;chk7;chk3), then 3 check boxes will get selected

You can try this : 您可以尝试以下方法:

public void SelectList(string[] selectedText){

  foreach(string item in selectedText){
     CheckBoxList1.Items.FindByText(item).Selected = true;
     // User FindByValue in case of to find the item via value.
  }

}

Call function like this : 调用函数如下:

SelectedList(new[]{ "text1", "text2" });

Or if you want it like dynamic n-array use params than method prototype would be 或者,如果您想要像动态n数组那样使用参数,则比方法原型更合适

public void SelectList(params string[] selectedText)

and calling procedure would be 和调用过程将是

SelectedList("text1", "text2");

Hope this will help. 希望这会有所帮助。

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

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