0

I'm creating some checkbox's from codebehind (adding through Panel.Controls.Add()). My question is: How can i modify the values?

I've already tried creating the control, use the method FindControl and them change some properties but with no sucess.

 CheckBox c = new CheckBox();
 c.FindControl("CheckBoxP");
 c.Checked = true;

Any ideas? Thanks

Gui
  • 9,555
  • 10
  • 42
  • 54

2 Answers2

1
    CheckBox _C = (CheckBox)this.Controls.Find("checkBox1", true).FirstOrDefault();
    if (_C != null)
    {
        _C.Checked = true;
    }

replace the 'checkBox1' with the name of the desired control

bodee
  • 2,654
  • 1
  • 16
  • 15
0

Try something like this (assuming you're using Windows Forms):

    foreach (Control c in this.Controls)
    {
        if (c.Name == "MyName" && c is CheckBox)
        {
            ((CheckBox)c).Checked = true;
        }
    }
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • this may be not works, cause the CheckBox is nested somewhere deeper in another container control (e.g. TableLayoutPanel, SplitContainer, etc.) – Oliver Jul 16 '10 at 12:42
  • Yeah thats true, it assumes you know which control your target control is on. – Iain Ward Jul 16 '10 at 13:04