Better way to find control in ASP.NET

后端 未结 9 1286
太阳男子
太阳男子 2020-11-22 03:17

I have a complex asp.net form,having even 50 to 60 fields in one form like there is Multiview, inside MultiView I have a GridView, and inside GridV

9条回答
  •  温柔的废话
    2020-11-22 03:44

    Late as usual. If anyone is still interested in this there are a number of related SO questions and answers. My version of recursive extension method for resolving this:

    public static IEnumerable FindControlsOfType(this Control parent)
                                                            where T : Control
    {
        foreach (Control child in parent.Controls)
        {
            if (child is T)
            {
                yield return (T)child;
            }
            else if (child.Controls.Count > 0)
            {
                foreach (T grandChild in child.FindControlsOfType())
                {
                    yield return grandChild;
                }
            }
        }
    }
    

提交回复
热议问题