C# - FindControl by ID and modify some properties
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 c开发者_StackOverflow社区ontrol, 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
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
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;
}
}
精彩评论