gpt4 book ai didi

C# LoadControl() (.ascx) 并添加到 "this"而不是子控件

转载 作者:行者123 更新时间:2023-11-30 22:28:08 26 4
gpt4 key购买 nike

我擅长加载控件,使用 LoadControl("~/vitrualPath"),所以我有:

UserControl ctrl = (UserControl)LoadControl("~/controls/someControl.ascx");
this.Controls.Add(ctrl);
//plcCtrl.Controls.Add(ctrl);

问题是我希望循环遍历用户控件中的所有控件:

foreach (Label c in this.Controls.OfType<Label>())
{
// It's a label for an input
if (c.ID.Substring(0, 8) == "lblInput")
{
// Do some stuff with the control here
}
}

但是,添加的控件不是this的一部分,而是ctrl的一部分

有没有一种方法可以将加载的控件的内容添加到 this 或者循环遍历 thisctrl一击?

最佳答案

如果您只想遍历顶级标签和 ctrl 中的标签, 尝试 this.Controls.Concat(ctrl.Controls).OfType<Label>()在你的foreach循环。

您还可以移动您的 if进入 LINQ Where调用:

.Where(l => l.ID.Substring(0, 8) == "lblInput")

关于C# LoadControl() (.ascx) 并添加到 "this"而不是子控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com