2 // System.Web.UI.MasterPage.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 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.
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.ComponentModel;
37 using System.Web.Compilation;
38 using System.Web.Util;
39 using System.Web.UI.WebControls;
42 namespace System.Web.UI
44 [ParseChildren (false)]
48 [ControlBuilder (typeof (MasterPageControlBuilder))]
49 public class MasterPage: UserControl
51 Hashtable definedContentTemplates = new Hashtable ();
52 Hashtable templates = new Hashtable ();
53 List <string> placeholders;
54 string parentMasterPageFile = null;
55 MasterPage parentMasterPage;
57 [EditorBrowsable (EditorBrowsableState.Advanced)]
58 protected internal void AddContentTemplate (string templateName, ITemplate template)
60 // LAMESPEC: should be ArgumentException
61 if (definedContentTemplates.ContainsKey (templateName))
62 throw new HttpException ("Multiple contents applied to " + templateName);
64 definedContentTemplates [templateName] = template;
68 [EditorBrowsable (EditorBrowsableState.Advanced)]
69 protected internal IList ContentPlaceHolders {
71 if (placeholders == null)
72 placeholders = new List <string> ();
78 [EditorBrowsable (EditorBrowsableState.Advanced)]
79 protected internal IDictionary ContentTemplates {
80 get { return templates; }
83 [DefaultValueAttribute ("")]
84 public string MasterPageFile {
85 get { return parentMasterPageFile; }
87 parentMasterPageFile = value;
88 parentMasterPage = null;
92 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93 [BrowsableAttribute (false)]
94 public MasterPage Master {
96 if (parentMasterPage == null && parentMasterPageFile != null)
97 parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
99 return parentMasterPage;
103 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
104 string masterPageFile, IDictionary contentTemplateCollection)
107 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
108 owner.Page.MapPath (masterPageFile),
111 MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
113 if (masterPage == null)
114 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
116 if (contentTemplateCollection != null) {
117 foreach (string templateName in contentTemplateCollection.Keys) {
118 if (masterPage.ContentTemplates [templateName] == null)
119 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
123 masterPage.Page = owner.Page;
124 masterPage.InitializeAsUserControlInternal ();
126 List <string> placeholders = masterPage.placeholders;
127 if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
128 foreach (string templateName in contentTemplateCollection.Keys) {
129 if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
130 throw new HttpException (
131 String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
132 templateName, masterPageFile));
140 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
142 /* XXX need to use virtual paths here? */
143 if (master.MasterPageFile != null) {
144 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
145 throw new HttpException ("circular dependency in master page files detected");
146 if (master.Master != null) {
147 master.Controls.Clear ();
148 master.Controls.Add (master.Master);
149 appliedMasterPageFiles.Add (master.MasterPageFile);
150 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);