1

如何从 C# 中的字符串名称中获取对控件的引用?

4

2 回答 2

9

Page.FindControl

如果控件是嵌套的,Control.FindControl请从父控件中使用。否则,您必须编写自己的FindControlRecursive

于 2009-01-30T04:31:56.257 回答
1
        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);
        }
于 2011-04-29T17:54:47.420 回答