开发者

C# - FindControl by ID and modify some properties

开发者 https://www.devze.com 2023-01-07 19:56 出处:网络
I\'m creating some checkbox\'s from codebehind (adding through Panel.Controls.Add()). My question is: How can i modify the values?

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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消