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 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
102 string masterPageFile, IDictionary contentTemplateCollection)
105 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
106 owner.Page.MapPath (masterPageFile),
109 MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
111 if (masterPage == null)
112 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
114 if (contentTemplateCollection != null) {
115 foreach (string templateName in contentTemplateCollection.Keys) {
116 if (masterPage.ContentTemplates [templateName] == null)
117 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
121 masterPage.Page = owner.Page;
122 masterPage.InitializeAsUserControlInternal ();
124 List <string> placeholders = masterPage.placeholders;
125 if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
126 foreach (string templateName in contentTemplateCollection.Keys) {
127 if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
128 throw new HttpException (
129 String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
130 templateName, masterPageFile));
138 internal static void ApplyMasterPageRecursive (MasterPage master, List <string> appliedMasterPageFiles)
140 /* XXX need to use virtual paths here? */
141 if (master.MasterPageFile != null) {
142 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
143 throw new HttpException ("circular dependency in master page files detected");
144 if (master.Master != null) {
145 master.Controls.Clear ();
146 master.Controls.Add (master.Master);
147 appliedMasterPageFiles.Add (master.MasterPageFile);
148 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);