2 // System.Web.UI.MasterPage.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.ComponentModel;
35 using System.Web.Compilation;
36 using System.Web.Util;
37 using System.Web.UI.WebControls;
40 namespace System.Web.UI
42 [ParseChildren (false)]
46 [ControlBuilder (typeof (MasterPageControlBuilder))]
47 public class MasterPage: UserControl
49 Hashtable definedContentTemplates = new Hashtable ();
50 Hashtable templates = new Hashtable ();
51 List <string> placeholders;
52 string parentMasterPageFile = null;
53 MasterPage parentMasterPage;
55 [EditorBrowsable (EditorBrowsableState.Advanced)]
56 protected internal void AddContentTemplate (string templateName, ITemplate template)
58 // LAMESPEC: should be ArgumentException
59 if (definedContentTemplates.ContainsKey (templateName))
60 throw new HttpException ("Multiple contents applied to " + templateName);
62 definedContentTemplates [templateName] = template;
66 [EditorBrowsable (EditorBrowsableState.Advanced)]
67 protected internal IList ContentPlaceHolders {
69 if (placeholders == null)
70 placeholders = new List <string> ();
76 [EditorBrowsable (EditorBrowsableState.Advanced)]
77 protected internal IDictionary ContentTemplates {
78 get { return templates; }
81 [DefaultValueAttribute ("")]
82 public string MasterPageFile {
83 get { return parentMasterPageFile; }
85 parentMasterPageFile = value;
86 parentMasterPage = null;
90 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91 [BrowsableAttribute (false)]
92 public MasterPage Master {
94 if (parentMasterPage == null && parentMasterPageFile != null)
95 parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
97 return parentMasterPage;
101 public void InstantiateInContentPlaceHolder (Control contentPlaceHolder, ITemplate template)
103 // .NET compatibility...
104 if (contentPlaceHolder == null || template == null)
105 throw new NullReferenceException ();
107 if (contentPlaceHolder != null && template != null)
108 template.InstantiateIn (contentPlaceHolder);
111 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
112 string masterPageFile, IDictionary contentTemplateCollection)
115 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
116 owner.Page.MapPath (masterPageFile),
119 MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
121 if (masterPage == null)
122 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
124 if (contentTemplateCollection != null) {
125 foreach (string templateName in contentTemplateCollection.Keys) {
126 if (masterPage.ContentTemplates [templateName] == null)
127 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
131 masterPage.Page = owner.Page;
132 masterPage.InitializeAsUserControlInternal ();
134 List <string> placeholders = masterPage.placeholders;
135 if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
136 foreach (string templateName in contentTemplateCollection.Keys) {
137 if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
138 throw new HttpException (
139 String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
140 templateName, masterPageFile));
148 internal static void ApplyMasterPageRecursive (MasterPage master, List <string> appliedMasterPageFiles)
150 /* XXX need to use virtual paths here? */
151 if (master.MasterPageFile != null) {
152 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
153 throw new HttpException ("circular dependency in master page files detected");
154 if (master.Master != null) {
155 master.Controls.Clear ();
156 master.Controls.Add (master.Master);
157 appliedMasterPageFiles.Add (master.MasterPageFile);
158 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);