How to get a reference to a control from its' string name in C#

…衆ロ難τιáo~ 提交于 2019-12-11 06:48:03

问题


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


来源:https://stackoverflow.com/questions/494487/how-to-get-a-reference-to-a-control-from-its-string-name-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!