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;
40 namespace System.Web.UI {
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
48 public abstract class TemplateControl : Control, INamingContainer {
50 static readonly Assembly _System_Web_Assembly = typeof (TemplateControl).Assembly;
51 static object abortTransaction = new object ();
52 static object commitTransaction = new object ();
53 static object error = new object ();
54 static string [] methodNames = { "Page_Init",
59 "Page_PreRenderComplete",
60 "Page_SaveStateComplete",
69 "Page_AbortTransaction",
70 "Page_CommitTransaction"};
72 const BindingFlags bflags = BindingFlags.Public |
73 BindingFlags.NonPublic |
74 BindingFlags.Instance;
77 protected TemplateControl ()
85 [EditorBrowsable (EditorBrowsableState.Never)]
89 protected virtual int AutoHandlers {
94 [EditorBrowsable (EditorBrowsableState.Never)]
95 protected virtual bool SupportAutoEvents {
101 [MonoTODO ("NotImplementedException")]
102 public string AppRelativeVirtualPath {
103 get { throw new NotImplementedException(); }
104 set { throw new NotImplementedException (); }
112 protected virtual void Construct ()
116 [MonoTODO ("Not implemented")]
117 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
124 internal void WireupAutomaticEvents ()
126 if (!SupportAutoEvents || !AutoEventWireup)
129 foreach (string methodName in methodNames) {
130 MethodInfo method = null;
132 for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
133 method = type.GetMethod (methodName, bflags);
140 if (method.DeclaringType != type) {
141 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
142 !method.IsFamilyAndAssembly && !method.IsFamily)
146 if (method.ReturnType != typeof (void))
149 ParameterInfo [] parms = method.GetParameters ();
150 int length = parms.Length;
151 bool noParams = (length == 0);
152 if (!noParams && (length != 2 ||
153 parms [0].ParameterType != typeof (object) ||
154 parms [1].ParameterType != typeof (EventArgs)))
157 int pos = methodName.IndexOf ("_");
158 string eventName = methodName.Substring (pos + 1);
159 EventInfo evt = type.GetEvent (eventName);
161 /* This should never happen */
166 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
167 evt.AddEventHandler (this, npi.FakeDelegate);
169 evt.AddEventHandler (this, Delegate.CreateDelegate (
170 typeof (EventHandler), this, methodName));
175 [EditorBrowsable (EditorBrowsableState.Never)]
176 protected virtual void FrameworkInitialize ()
180 Type GetTypeFromControlPath (string virtualPath)
182 if (virtualPath == null)
183 throw new ArgumentNullException ("virtualPath");
185 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
186 string realpath = Context.Request.MapPath (vpath);
187 return UserControlParser.GetCompiledType (vpath, realpath, Context);
190 public Control LoadControl (string virtualPath)
193 if (virtualPath == null)
194 throw new ArgumentNullException ("virtualPath");
196 if (virtualPath == null)
197 throw new HttpException ("virtualPath is null");
199 Type type = GetTypeFromControlPath (virtualPath);
200 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
201 if (attrs != null && attrs.Length == 1) {
202 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
203 PartialCachingControl ctrl = new PartialCachingControl (type);
204 ctrl.VaryByParams = attr.VaryByParams;
205 ctrl.VaryByControls = attr.VaryByControls;
206 ctrl.VaryByCustom = attr.VaryByCustom;
210 object control = Activator.CreateInstance (type);
211 if (control is UserControl)
212 ((UserControl) control).InitializeAsUserControl (Page);
214 return (Control) control;
217 public ITemplate LoadTemplate (string virtualPath)
220 if (virtualPath == null)
221 throw new ArgumentNullException ("virtualPath");
223 if (virtualPath == null)
224 throw new HttpException ("virtualPath is null");
226 Type t = GetTypeFromControlPath (virtualPath);
227 return new SimpleTemplate (t);
230 protected virtual void OnAbortTransaction (EventArgs e)
232 EventHandler eh = Events [abortTransaction] as EventHandler;
237 protected virtual void OnCommitTransaction (EventArgs e)
239 EventHandler eh = Events [commitTransaction] as EventHandler;
244 protected virtual void OnError (EventArgs e)
246 EventHandler eh = Events [error] as EventHandler;
251 [MonoTODO ("Not implemented, always returns null")]
252 public Control ParseControl (string content)
255 throw new ArgumentNullException ("content");
260 [EditorBrowsable (EditorBrowsableState.Never)]
265 object ReadStringResource ()
267 throw new NotSupportedException ();
271 protected object GetGlobalResourceObject (string className, string resourceKey)
273 return HttpContext.GetGlobalResourceObject (className, resourceKey);
276 [MonoTODO ("Not implemented")]
277 protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
279 // FIXME: not sure how to implement that one yet
280 throw new NotSupportedException();
283 protected object GetLocalResourceObject (string resourceKey)
285 return HttpContext.GetLocalResourceObject (Context.Request.CurrentExecutionFilePath, resourceKey);
288 protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
290 // FIXME: not sure how to implement that one yet
291 throw new NotSupportedException();
295 [EditorBrowsable (EditorBrowsableState.Never)]
296 public static object ReadStringResource (Type t)
298 throw new NotSupportedException ();
301 [MonoTODO ("Not implemented, does nothing")]
302 [EditorBrowsable (EditorBrowsableState.Never)]
303 protected void SetStringResourcePointer (object stringResourcePointer,
304 int maxResourceOffset)
308 [MonoTODO ("Not implemented, does nothing")]
309 [EditorBrowsable (EditorBrowsableState.Never)]
310 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
311 int size, bool fAsciiOnly)
319 [WebSysDescription ("Raised when the user aborts a transaction.")]
320 public event EventHandler AbortTransaction {
321 add { Events.AddHandler (abortTransaction, value); }
322 remove { Events.RemoveHandler (abortTransaction, value); }
325 [WebSysDescription ("Raised when the user initiates a transaction.")]
326 public event EventHandler CommitTransaction {
327 add { Events.AddHandler (commitTransaction, value); }
328 remove { Events.RemoveHandler (commitTransaction, value); }
331 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
332 public event EventHandler Error {
333 add { Events.AddHandler (error, value); }
334 remove { Events.RemoveHandler (error, value); }
339 class SimpleTemplate : ITemplate
343 public SimpleTemplate (Type type)
348 public void InstantiateIn (Control control)
350 Control template = Activator.CreateInstance (type) as Control;
351 template.SetBindingContainer (false);
352 control.Controls.Add (template);
357 protected internal object Eval (string expression)
359 return DataBinder.Eval (Page.GetDataItem(), expression);
362 protected internal string Eval (string expression, string format)
364 return DataBinder.Eval (Page.GetDataItem(), expression, format);
367 protected internal object XPath (string xpathexpression)
369 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
372 protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
374 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
377 protected internal string XPath (string xpathexpression, string format)
379 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
382 protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
384 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
387 protected internal IEnumerable XPathSelect (string xpathexpression)
389 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
392 protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
394 return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
397 // IFilterResolutionService
399 [MonoTODO ("Not implemented")]
400 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
402 throw new NotImplementedException ();
405 [MonoTODO ("Not implemented")]
406 bool IFilterResolutionService.EvaluateFilter (string filterName)
408 throw new NotImplementedException ();