Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David 2 posts 22 karma points
    Jun 01, 2014 @ 18:41
    David
    0

    Getting data/input from several User Controls?

    Hi all. I have Googled this a lot without success. Plese help.

    I want to collect the user input from several User Controls and process the input in code behind.

    Is that possible??

     

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 02, 2014 @ 10:17
    Ismail Mayat
    0

    David,

    Not really umbraco related more .net related, so what you would need is code behind for the master template for the page that contains the usercontrols, then in that code behind do a recursive look for all the fields and then get their values,

            /// <summary>
            /// get find control method using generics
            /// </summary>
            /// <typeparam name="T">type of item you want</typeparam>
            /// <param name="ancestor">parent to start looking from</param>
            /// <returns>collection of T items</returns>
            public static List<T> ControlsByTypeUsingAncestor<T>(Control ancestor) where T : Control
            {
                var controls = new List<T>();
    
                //Append to search results if we match the type 
                if (typeof (T).IsAssignableFrom(ancestor.GetType()))
                {
                    controls.Add((T) ancestor);
                }
    
                //Recurse into child controls 
                foreach (Control ctrl in ancestor.Controls)
                {
                    controls.AddRange(ControlsByTypeUsingAncestor<T>(ctrl));
                }
    
                return controls;
            }

    so you could do something like

    var textboxes = ControlsByTypeUsingAncestor<TextBox>(this);

    where this is the current master page object, so the code will recursively get you all textboxes on the current page, you can repeat for other controls.

    Regards


    Ismail

     

Please Sign in or register to post replies

Write your reply to:

Draft