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;
41 namespace System.Web.UI
43 [ParseChildren (false)]
47 [ControlBuilder (typeof (MasterPageControlBuilder))]
48 public class MasterPage: UserControl
50 Hashtable definedContentTemplates = new Hashtable ();
51 Hashtable templates = new Hashtable ();
52 ArrayList placeholders = new ArrayList ();
53 List <string> placeholderIds;
55 string parentMasterPageFile = null;
56 MasterPage parentMasterPage;
58 [EditorBrowsable (EditorBrowsableState.Advanced)]
59 protected internal void AddContentTemplate (string templateName, ITemplate template)
61 // LAMESPEC: should be ArgumentException
62 if (definedContentTemplates.ContainsKey (templateName))
63 throw new HttpException ("Multiple contents applied to " + templateName);
65 definedContentTemplates [templateName] = template;
69 [EditorBrowsable (EditorBrowsableState.Advanced)]
70 protected internal IList ContentPlaceHolders {
71 get { return placeholders; }
75 [EditorBrowsable (EditorBrowsableState.Advanced)]
76 protected internal IDictionary ContentTemplates {
77 get { return templates; }
80 [DefaultValueAttribute ("")]
81 public string MasterPageFile {
82 get { return parentMasterPageFile; }
84 parentMasterPageFile = value;
85 parentMasterPage = null;
89 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90 [BrowsableAttribute (false)]
91 public MasterPage Master {
93 if (parentMasterPage == null && parentMasterPageFile != null)
94 parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
96 return parentMasterPage;
100 internal void SetPlaceHolderIds (List <string> ids)
102 placeholderIds = ids;
105 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
106 string masterPageFile, IDictionary contentTemplateCollection)
109 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
110 owner.Page.MapPath (masterPageFile),
113 MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
115 if (masterPage == null)
116 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
118 List <string> phids = masterPage.placeholderIds;
119 if (contentTemplateCollection != null) {
120 foreach (string templateName in contentTemplateCollection.Keys) {
121 if (phids != null && !phids.Contains (templateName))
122 throw new HttpException (
123 String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
124 templateName, masterPageFile));
125 if (masterPage.ContentTemplates [templateName] == null)
126 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
129 masterPage.Page = owner.Page;
130 masterPage.InitializeAsUserControlInternal ();
134 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
136 /* XXX need to use virtual paths here? */
137 if (master.MasterPageFile != null) {
138 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
139 throw new HttpException ("circular dependency in master page files detected");
140 if (master.Master != null) {
141 master.Controls.Clear ();
142 master.Controls.Add (master.Master);
143 appliedMasterPageFiles.Add (master.MasterPageFile);
144 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);