How can I iterate through all checkboxes on a form?

后端 未结 6 873
[愿得一人]
[愿得一人] 2020-11-29 08:36

I have a form that has many dynamically generated checkboxes. At runtime, how can I iterate through each of them so I can get their value and IDs?

6条回答
  •  温柔的废话
    2020-11-29 08:51

    I know that this is old, but It was easy as I can imagine.

    Just add all checkboxes into a List, all checkboxes state are in the list and even if they change in the UI in the list changes too.

    List checkboxes = new List();
    checkboxes.Add(chk1);
    checkboxes.Add(chk2);
    //So add all checkboxes you wanna iterate
    
    foreach(Checkbox checkbox in checkboxes){
        //Do something using checkbox object
    }
    

    Hope this helps :)

提交回复
热议问题