Return to Snippet

Revision: 17951
at September 17, 2009 14:29 by laurenceosx


Updated Code
//
        private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            // add this to aspx page that needs to load a .ascx control
            // http://www.effegidev.com/post/WebUserControls-and-Parameters.aspx
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }
            UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;
            // Find the relevant constructor
            ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());
            //And then call the relevant constructor
            if (constructor == null)
            {
                throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
            }
            else
            {
                constructor.Invoke(ctl, constructorParameters);
            }
            // Finally return the fully initialized UC
            return ctl;
        } 
        //

Revision: 17950
at September 17, 2009 14:27 by laurenceosx


Initial Code
//
        private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            // http://www.effegidev.com/post/WebUserControls-and-Parameters.aspx
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }
            UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;
            // Find the relevant constructor
            ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());
            //And then call the relevant constructor
            if (constructor == null)
            {
                throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
            }
            else
            {
                constructor.Invoke(ctl, constructorParameters);
            }
            // Finally return the fully initialized UC
            return ctl;
        } 
        //

Initial URL
http://www.effegidev.com/post/WebUserControls-and-Parameters.aspx

Initial Description

                                

Initial Title
Aspx Load User Control with Params

Initial Tags

                                

Initial Language
C#