Better way to find control in ASP.NET

后端 未结 9 1297
太阳男子
太阳男子 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条回答
  •  -上瘾入骨i
    2020-11-22 03:55

    If you're looking for a specific type of control you could use a recursive loop like this one - http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx

    Here's an example I made that returns all controls of the given type

    /// 
    /// Finds all controls of type T stores them in FoundControls
    /// 
    /// 
    private class ControlFinder where T : Control 
    {
        private readonly List _foundControls = new List();
        public IEnumerable FoundControls
        {
            get { return _foundControls; }
        }    
    
        public void FindChildControlsRecursive(Control control)
        {
            foreach (Control childControl in control.Controls)
            {
                if (childControl.GetType() == typeof(T))
                {
                    _foundControls.Add((T)childControl);
                }
                else
                {
                    FindChildControlsRecursive(childControl);
                }
            }
        }
    }
    

提交回复
热议问题