How to get a reference to a control from its' string name in C#?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Page.FindControl
If the Control is nested, use Control.FindControl
from the parent Control. Otherwise, you'll have to write your own FindControlRecursive
回答2:
private Control FindControlRecursive(Control root, string id)
{
return root.ID == id
? root
: (root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id)))
.FirstOrDefault(t => t != null);
}