2 // System.Web.UI.TemplateControl.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Reflection;
35 using System.Security.Permissions;
36 using System.Web.Compilation;
37 using System.Web.Util;
39 namespace System.Web.UI {
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
47 public abstract class TemplateControl : Control, INamingContainer {
49 static object abortTransaction = new object ();
50 static object commitTransaction = new object ();
51 static object error = new object ();
52 static string [] methodNames = { "Page_Init",
57 "Page_PreRenderComplete",
58 "Page_SaveStateComplete",
67 "Page_AbortTransaction",
68 "Page_CommitTransaction"};
70 const BindingFlags bflags = BindingFlags.Public |
71 BindingFlags.NonPublic |
72 BindingFlags.Instance;
75 protected TemplateControl ()
83 [EditorBrowsable (EditorBrowsableState.Never)]
87 protected virtual int AutoHandlers {
92 [EditorBrowsable (EditorBrowsableState.Never)]
93 protected virtual bool SupportAutoEvents {
101 protected virtual void Construct ()
106 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
113 internal void WireupAutomaticEvents ()
115 if (!SupportAutoEvents || !AutoEventWireup)
118 Type type = GetType ();
119 foreach (string methodName in methodNames) {
120 MethodInfo method = type.GetMethod (methodName, bflags);
124 if (method.DeclaringType != type) {
125 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
126 !method.IsFamilyAndAssembly && !method.IsFamily)
130 if (method.ReturnType != typeof (void))
133 ParameterInfo [] parms = method.GetParameters ();
134 int length = parms.Length;
135 bool noParams = (length == 0);
136 if (!noParams && (length != 2 ||
137 parms [0].ParameterType != typeof (object) ||
138 parms [1].ParameterType != typeof (EventArgs)))
141 int pos = methodName.IndexOf ("_");
142 string eventName = methodName.Substring (pos + 1);
143 EventInfo evt = type.GetEvent (eventName);
145 /* This should never happen */
150 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
151 evt.AddEventHandler (this, npi.FakeDelegate);
153 evt.AddEventHandler (this, Delegate.CreateDelegate (
154 typeof (EventHandler), this, methodName));
159 [EditorBrowsable (EditorBrowsableState.Never)]
160 protected virtual void FrameworkInitialize ()
164 Type GetTypeFromControlPath (string virtualPath)
166 if (virtualPath == null)
167 throw new ArgumentNullException ("virtualPath");
169 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
170 string realpath = Context.Request.MapPath (vpath);
171 return UserControlParser.GetCompiledType (vpath, realpath, Context);
174 public Control LoadControl (string virtualPath)
177 if (virtualPath == null)
178 throw new ArgumentNullException ("virtualPath");
180 if (virtualPath == null)
181 throw new HttpException ("virtualPath is null");
183 Type type = GetTypeFromControlPath (virtualPath);
184 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
185 if (attrs != null && attrs.Length == 1) {
186 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
187 PartialCachingControl ctrl = new PartialCachingControl (type);
188 ctrl.VaryByParams = attr.VaryByParams;
189 ctrl.VaryByControls = attr.VaryByControls;
190 ctrl.VaryByCustom = attr.VaryByCustom;
194 object control = Activator.CreateInstance (type);
195 if (control is UserControl)
196 ((UserControl) control).InitializeAsUserControl (Page);
198 return (Control) control;
201 public ITemplate LoadTemplate (string virtualPath)
204 if (virtualPath == null)
205 throw new ArgumentNullException ("virtualPath");
207 if (virtualPath == null)
208 throw new HttpException ("virtualPath is null");
210 Type t = GetTypeFromControlPath (virtualPath);
211 return new SimpleTemplate (t);
214 protected virtual void OnAbortTransaction (EventArgs e)
216 EventHandler eh = Events [abortTransaction] as EventHandler;
221 protected virtual void OnCommitTransaction (EventArgs e)
223 EventHandler eh = Events [commitTransaction] as EventHandler;
228 protected virtual void OnError (EventArgs e)
230 EventHandler eh = Events [error] as EventHandler;
236 public Control ParseControl (string content)
239 throw new ArgumentNullException ("content");
244 [EditorBrowsable (EditorBrowsableState.Never)]
249 object ReadStringResource ()
251 throw new NotSupportedException ();
254 [EditorBrowsable (EditorBrowsableState.Never)]
255 public static object ReadStringResource (Type t)
257 throw new NotSupportedException ();
261 [EditorBrowsable (EditorBrowsableState.Never)]
262 protected void SetStringResourcePointer (object stringResourcePointer,
263 int maxResourceOffset)
268 [EditorBrowsable (EditorBrowsableState.Never)]
269 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
270 int size, bool fAsciiOnly)
278 [WebSysDescription ("Raised when the user aborts a transaction.")]
279 public event EventHandler AbortTransaction {
280 add { Events.AddHandler (abortTransaction, value); }
281 remove { Events.RemoveHandler (abortTransaction, value); }
284 [WebSysDescription ("Raised when the user initiates a transaction.")]
285 public event EventHandler CommitTransaction {
286 add { Events.AddHandler (commitTransaction, value); }
287 remove { Events.RemoveHandler (commitTransaction, value); }
290 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
291 public event EventHandler Error {
292 add { Events.AddHandler (error, value); }
293 remove { Events.RemoveHandler (error, value); }
298 class SimpleTemplate : ITemplate
302 public SimpleTemplate (Type type)
307 public void InstantiateIn (Control control)
309 Control template = Activator.CreateInstance (type) as Control;
310 template.SetBindingContainer (false);
311 control.Controls.Add (template);
316 protected object Eval (string expression)
318 return DataBinder.Eval (Page.GetDataItem(), expression);
321 protected string Eval (string expression, string format)
323 return DataBinder.Eval (Page.GetDataItem(), expression, format);
326 protected object XPath (string xpathexpression)
328 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
331 protected string XPath (string xpathexpression, string format)
333 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
336 protected IEnumerable XPathSelect (string xpathexpression)
338 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
341 // IFilterResolutionService
344 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
346 throw new NotImplementedException ();
350 bool IFilterResolutionService.EvaluateFilter (string filterName)
352 throw new NotImplementedException ();