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.Util;
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 ArrayList placeholders = new ArrayList ();
52 List <string> placeholderIds;
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 {
70 get { return placeholders; }
74 [EditorBrowsable (EditorBrowsableState.Advanced)]
75 protected internal IDictionary ContentTemplates {
76 get { return templates; }
79 [DefaultValueAttribute ("")]
80 public string MasterPageFile {
81 get { return parentMasterPageFile; }
83 parentMasterPageFile = value;
84 parentMasterPage = null;
88 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89 [BrowsableAttribute (false)]
90 public MasterPage Master {
92 if (parentMasterPage == null && parentMasterPageFile != null)
93 parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
95 return parentMasterPage;
99 internal void SetPlaceHolderIds (List <string> ids)
101 placeholderIds = ids;
104 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
105 string masterPageFile, IDictionary contentTemplateCollection)
107 MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
108 owner.Page.MapPath (masterPageFile),
110 List <string> phids = masterPage.placeholderIds;
111 if (contentTemplateCollection != null) {
112 foreach (string templateName in contentTemplateCollection.Keys) {
113 if (phids != null && !phids.Contains (templateName))
114 throw new HttpException (
115 String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
116 templateName, masterPageFile));
117 if (masterPage.ContentTemplates [templateName] == null)
118 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
121 masterPage.Page = owner.Page;
122 masterPage.InitializeAsUserControlInternal ();
126 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
128 /* XXX need to use virtual paths here? */
129 if (master.MasterPageFile != null) {
130 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
131 throw new HttpException ("circular dependency in master page files detected");
132 if (master.Master != null) {
133 master.Controls.Clear ();
134 master.Controls.Add (master.Master);
135 appliedMasterPageFiles.Add (master.MasterPageFile);
136 MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);