Better way to find control in ASP.NET

后端 未结 9 1283
太阳男子
太阳男子 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<T> FindControlsOfType<T>(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<T>())
                {
                    yield return grandChild;
                }
            }
        }
    }
    
    0 讨论(0)
  • 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

    /// <summary>
    /// Finds all controls of type T stores them in FoundControls
    /// </summary>
    /// <typeparam name="T"></typeparam>
    private class ControlFinder<T> where T : Control 
    {
        private readonly List<T> _foundControls = new List<T>();
        public IEnumerable<T> 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);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:57

    Action Management On Controls

    Create below class in base class. Class To get all controls:

    public static class ControlExtensions
    {
        public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control
        {
            var result = new List<T>();
            foreach (Control control in parent.Controls)
            {
                if (control is T)
                {
                    result.Add((T)control);
                }
                if (control.HasControls())
                {
                    result.AddRange(control.GetAllControlsOfType<T>());
                }
            }
            return result;
        }
    }
    

    From Database: Get All Actions IDs (like divAction1,divAction2 ....) dynamic in DATASET (DTActions) allow on specific User.

    In Aspx: in HTML Put Action(button,anchor etc) in div or span and give them id like

    <div id="divAction1" visible="false" runat="server" clientidmode="Static">   
                    <a id="anchorAction" runat="server">Submit
                            </a>                      
                     </div>
    

    IN CS: Use this function on your page:

    private void ShowHideActions()
        {
    
            var controls = Page.GetAllControlsOfType<HtmlGenericControl>();
    
            foreach (DataRow dr in DTActions.Rows)
            {          
    
                foreach (Control cont in controls)
                {
    
                    if (cont.ClientID == "divAction" + dr["ActionID"].ToString())
                    {
                        cont.Visible = true;
                    }
    
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 03:57

    https://blog.codinghorror.com/recursive-pagefindcontrol/

    Page.FindControl("DataList1:_ctl0:TextBox3");
    

    OR

    private Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }
        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-11-22 03:58

    Recursively find all controls matching the specified predicate (do not include root Control):

        public static IEnumerable<Control> FindControlsRecursive(this Control control, Func<Control, bool> predicate)
        {
            var results = new List<Control>();
    
            foreach (Control child in control.Controls)
            {
                if (predicate(child))
                {
                    results.Add(child);
                }
                results.AddRange(child.FindControlsRecursive(predicate));
            }
    
            return results;
        }
    

    Usage:

    myControl.FindControlsRecursive(c => c.ID == "findThisID");
    
    0 讨论(0)
  • 2020-11-22 04:00

    FindControl does not search within nested controls recursively. It does only find controls that's NamigContainer is the Control on that you are calling FindControl.

    Theres a reason that ASP.Net does not look into your nested controls recursively by default:

    • Performance
    • Avoiding errors
    • Reusability

    Consider you want to encapsulate your GridViews, Formviews, UserControls etc. inside of other UserControls for reusability reasons. If you would have implemented all logic in your page and accessed these controls with recursive loops, it'll very difficult to refactor that. If you have implemented your logic and access methods via the event-handlers(f.e. RowDataBound of GridView), it'll be much simpler and less error-prone.

    0 讨论(0)
提交回复
热议问题