internal static MasterPage CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection)
        {
            MasterPage child = null;

            if (masterPageFile == null)
            {
                if ((contentTemplateCollection != null) && (contentTemplateCollection.Count > 0))
                {
                    throw new HttpException(System.Web.SR.GetString("Content_only_allowed_in_content_page"));
                }
                return(null);
            }
            VirtualPath            virtualPath      = VirtualPathProvider.CombineVirtualPathsInternal(owner.TemplateControlVirtualPath, masterPageFile);
            ITypedWebObjectFactory vPathBuildResult = (ITypedWebObjectFactory)BuildManager.GetVPathBuildResult(context, virtualPath);

            if (!typeof(MasterPage).IsAssignableFrom(vPathBuildResult.InstantiatedType))
            {
                throw new HttpException(System.Web.SR.GetString("Invalid_master_base", new object[] { masterPageFile }));
            }
            child = (MasterPage)vPathBuildResult.CreateInstance();
            child.TemplateControlVirtualPath = virtualPath;
            if (owner.HasControls())
            {
                foreach (Control control in owner.Controls)
                {
                    LiteralControl control2 = control as LiteralControl;
                    if ((control2 == null) || (System.Web.UI.Util.FirstNonWhiteSpaceIndex(control2.Text) >= 0))
                    {
                        throw new HttpException(System.Web.SR.GetString("Content_allowed_in_top_level_only"));
                    }
                }
                owner.Controls.Clear();
            }
            if (owner.Controls.IsReadOnly)
            {
                throw new HttpException(System.Web.SR.GetString("MasterPage_Cannot_ApplyTo_ReadOnly_Collection"));
            }
            if (contentTemplateCollection != null)
            {
                foreach (string str in contentTemplateCollection.Keys)
                {
                    if (!child.ContentPlaceHolders.Contains(str.ToLower(CultureInfo.InvariantCulture)))
                    {
                        throw new HttpException(System.Web.SR.GetString("MasterPage_doesnt_have_contentplaceholder", new object[] { str, masterPageFile }));
                    }
                }
                child._contentTemplates = contentTemplateCollection;
            }
            child._ownerControl = owner;
            child.InitializeAsUserControl(owner.Page);
            owner.Controls.Add(child);
            return(child);
        }
Example #2
0
 /*
  * Turns relative virtual path into absolute ones
  */
 internal VirtualPath ResolveVirtualPath(VirtualPath virtualPath)
 {
     return(VirtualPathProvider.CombineVirtualPathsInternal(CurrentVirtualPath, virtualPath));
 }
        internal static MasterPage CreateMaster(TemplateControl owner, HttpContext context,
                                                VirtualPath masterPageFile, IDictionary contentTemplateCollection)
        {
            Debug.Assert(owner is MasterPage || owner is Page);

            MasterPage master = null;

            if (masterPageFile == null)
            {
                if (contentTemplateCollection != null && contentTemplateCollection.Count > 0)
                {
                    throw new HttpException(SR.GetString(SR.Content_only_allowed_in_content_page));
                }

                return(null);
            }

            // If it's relative, make it *app* relative.  Treat is as relative to this
            // user control (ASURT 55513)
            VirtualPath virtualPath = VirtualPathProvider.CombineVirtualPathsInternal(
                owner.TemplateControlVirtualPath, masterPageFile);

            // Compile the declarative control and get its Type
            ITypedWebObjectFactory result = (ITypedWebObjectFactory)BuildManager.GetVPathBuildResult(
                context, virtualPath);

            // Make sure it has the correct base type
            if (!typeof(MasterPage).IsAssignableFrom(result.InstantiatedType))
            {
                throw new HttpException(SR.GetString(SR.Invalid_master_base,
                                                     masterPageFile));
            }

            master = (MasterPage)result.CreateInstance();

            master.TemplateControlVirtualPath = virtualPath;

            if (owner.HasControls())
            {
                foreach (Control control in owner.Controls)
                {
                    LiteralControl literal = control as LiteralControl;
                    if (literal == null || Util.FirstNonWhiteSpaceIndex(literal.Text) >= 0)
                    {
                        throw new HttpException(SR.GetString(SR.Content_allowed_in_top_level_only));
                    }
                }

                // Remove existing controls.
                owner.Controls.Clear();
            }

            // Make sure the control collection is writable.
            if (owner.Controls.IsReadOnly)
            {
                throw new HttpException(SR.GetString(SR.MasterPage_Cannot_ApplyTo_ReadOnly_Collection));
            }

            if (contentTemplateCollection != null)
            {
                foreach (String contentName in contentTemplateCollection.Keys)
                {
                    if (!master.ContentPlaceHolders.Contains(contentName.ToLower(CultureInfo.InvariantCulture)))
                    {
                        throw new HttpException(SR.GetString(SR.MasterPage_doesnt_have_contentplaceholder, contentName, masterPageFile));
                    }
                }
                master._contentTemplates = contentTemplateCollection;
            }

            master._ownerControl = owner;
            master.InitializeAsUserControl(owner.Page);
            owner.Controls.Add(master);
            return(master);
        }