Looping through a panel with unknown number of Radiobuttons/Checkboxes

codemonkey2480
codemonkey2480 used Ask the Experts™
on
I have panel that adds dynamic radiobuttons based on records from a database table.
Example: If table has departments D1,D2,D3 then there will be 3 radiobuttons on the UI.

I would like to be able to go through all the radiobuttons inside the panel and find out which one is checked. If say D1 is checked then get the text property value of that radio button.

If they are checkboxes and multiple selections are allowed, How can I approach that?

Please let me know
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can do this inside your panel:


 foreach (Control RadioBut in panel.Controls)
            {
                if (RadioBut is RadioButton)
                {
                    bool isChecked = (RadioBut as RadioButton).Checked;
                }
            }

hope this helps
And you can get text doing:


            foreach (Control RadioBut in this.Controls)
            {
                if (RadioBut is RadioButton)
                {
                    if ((RadioBut as RadioButton).Checked)
                    {
                        string value = (RadioBut as RadioButton).Text;
                    }
                }
            }

Open in new window

The same logic u have implemented for radio button can be implemented for check box also...

The only thing is concatenate the selected check boxes value using a splitter like...


string value;
for loop comes here ....

value  += chkBox1.selectedItem + "^^"

for loop ends here....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial