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.ComponentModel;
36 using System.Web.Util;
39 namespace System.Web.UI
41 [ParseChildren (false)]
45 [ControlBuilder (typeof (MasterPageControlBuilder))]
46 public class MasterPage: UserControl
48 Hashtable definedContentTemplates = new Hashtable ();
49 Hashtable templates = new Hashtable ();
50 ArrayList placeholders = new ArrayList ();
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 {
68 get { return placeholders; }
72 [EditorBrowsable (EditorBrowsableState.Advanced)]
73 protected internal IDictionary ContentTemplates {
74 get { return templates; }
77 [DefaultValueAttribute ("")]
78 public string MasterPageFile {
79 get { return parentMasterPageFile; }
81 parentMasterPageFile = value;
82 parentMasterPage = null;
86 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87 [BrowsableAttribute (false)]
88 public MasterPage Master {
90 if (parentMasterPage == null && parentMasterPageFile != null)
91 parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
93 return parentMasterPage;
97 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context, string masterPageFile, IDictionary contentTemplateCollection)
99 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile, owner.Page.MapPath (masterPageFile), context);
100 if (contentTemplateCollection != null) {
101 foreach (string templateName in contentTemplateCollection.Keys) {
102 if (masterPage.ContentTemplates[templateName] == null)
103 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
106 masterPage.Page = owner.Page;
107 masterPage.InitializeAsUserControlInternal ();
111 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
113 /* XXX need to use virtual paths here? */
114 if (master.MasterPageFile != null) {
115 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
116 throw new HttpException ("circular dependency in master page files detected");
117 if (master.Master != null) {
118 master.Controls.Clear ();
119 master.Controls.Add (master.Master);
120 appliedMasterPageFiles.Add (master.MasterPageFile);
121 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);