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;
41 namespace System.Web.UI {
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
49 public abstract class TemplateControl : Control, INamingContainer {
51 static readonly Assembly _System_Web_Assembly = typeof (TemplateControl).Assembly;
52 static object abortTransaction = new object ();
53 static object commitTransaction = new object ();
54 static object error = new object ();
55 static string [] methodNames = { "Page_Init",
60 "Page_PreRenderComplete",
61 "Page_SaveStateComplete",
70 "Page_AbortTransaction",
71 "Page_CommitTransaction"};
73 const BindingFlags bflags = BindingFlags.Public |
74 BindingFlags.NonPublic |
75 BindingFlags.Instance;
78 string _appRelativeVirtualPath;
82 protected TemplateControl ()
85 TemplateControl = this;
93 [EditorBrowsable (EditorBrowsableState.Never)]
97 protected virtual int AutoHandlers {
102 [EditorBrowsable (EditorBrowsableState.Never)]
103 protected virtual bool SupportAutoEvents {
108 public string AppRelativeVirtualPath {
109 get { return _appRelativeVirtualPath; }
110 set { _appRelativeVirtualPath = value; }
118 protected virtual void Construct ()
122 [MonoTODO ("Not implemented")]
123 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
130 internal void WireupAutomaticEvents ()
132 if (!SupportAutoEvents || !AutoEventWireup)
135 foreach (string methodName in methodNames) {
136 MethodInfo method = null;
138 for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
139 method = type.GetMethod (methodName, bflags);
146 if (method.DeclaringType != type) {
147 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
148 !method.IsFamilyAndAssembly && !method.IsFamily)
152 if (method.ReturnType != typeof (void))
155 ParameterInfo [] parms = method.GetParameters ();
156 int length = parms.Length;
157 bool noParams = (length == 0);
158 if (!noParams && (length != 2 ||
159 parms [0].ParameterType != typeof (object) ||
160 parms [1].ParameterType != typeof (EventArgs)))
163 int pos = methodName.IndexOf ("_");
164 string eventName = methodName.Substring (pos + 1);
165 EventInfo evt = type.GetEvent (eventName);
167 /* This should never happen */
172 NoParamsInvoker npi = new NoParamsInvoker (this, method);
173 evt.AddEventHandler (this, npi.FakeDelegate);
175 evt.AddEventHandler (this, Delegate.CreateDelegate (
177 typeof (EventHandler), this, method));
179 typeof (EventHandler), this, methodName));
185 [EditorBrowsable (EditorBrowsableState.Never)]
186 protected virtual void FrameworkInitialize ()
190 Type GetTypeFromControlPath (string virtualPath)
192 if (virtualPath == null)
193 throw new ArgumentNullException ("virtualPath");
195 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
196 string realpath = Context.Request.MapPath (vpath);
198 return BuildManager.GetCompiledType (vpath);
200 return UserControlParser.GetCompiledType (vpath, realpath, Context);
204 public Control LoadControl (string virtualPath)
207 if (virtualPath == null)
208 throw new ArgumentNullException ("virtualPath");
210 if (virtualPath == null)
211 throw new HttpException ("virtualPath is null");
213 Type type = GetTypeFromControlPath (virtualPath);
215 return LoadControl (type, null);
218 public Control LoadControl (Type type, object[] parameters)
220 object [] attrs = null;
223 type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
224 if (attrs != null && attrs.Length == 1) {
225 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
226 PartialCachingControl ctrl = new PartialCachingControl (type, parameters);
227 ctrl.VaryByParams = attr.VaryByParams;
228 ctrl.VaryByControls = attr.VaryByControls;
229 ctrl.VaryByCustom = attr.VaryByCustom;
233 object control = Activator.CreateInstance (type, parameters);
234 if (control is UserControl)
235 ((UserControl) control).InitializeAsUserControl (Page);
237 return (Control) control;
240 public ITemplate LoadTemplate (string virtualPath)
243 if (virtualPath == null)
244 throw new ArgumentNullException ("virtualPath");
246 if (virtualPath == null)
247 throw new HttpException ("virtualPath is null");
249 Type t = GetTypeFromControlPath (virtualPath);
250 return new SimpleTemplate (t);
253 protected virtual void OnAbortTransaction (EventArgs e)
255 EventHandler eh = Events [abortTransaction] as EventHandler;
260 protected virtual void OnCommitTransaction (EventArgs e)
262 EventHandler eh = Events [commitTransaction] as EventHandler;
267 protected virtual void OnError (EventArgs e)
269 EventHandler eh = Events [error] as EventHandler;
275 [MonoTODO ("Not implemented, always returns null")]
277 public Control ParseControl (string content)
280 throw new ArgumentNullException ("content");
283 TextReader reader = new StringReader (content);
284 Type control = UserControlParser.GetCompiledType (reader, HttpContext.Current);
288 return Activator.CreateInstance (control, null) as Control;
295 [MonoTODO ("Parser filters not implemented yet. Calls ParseControl (string) for now.")]
296 public Control ParseControl (string content, bool ignoreParserFilter)
298 return ParseControl (content);
302 [EditorBrowsable (EditorBrowsableState.Never)]
307 object ReadStringResource ()
309 throw new NotSupportedException ();
313 protected object GetGlobalResourceObject (string className, string resourceKey)
315 return HttpContext.GetGlobalResourceObject (className, resourceKey);
318 protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
320 if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) ||
321 String.IsNullOrEmpty (className) || objType == null)
324 object globalObject = GetGlobalResourceObject (className, resourceKey);
325 if (globalObject == null)
328 TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
329 if (converter == null || !converter.CanConvertFrom (globalObject.GetType ()))
332 return converter.ConvertFrom (globalObject);
335 protected object GetLocalResourceObject (string resourceKey)
337 return HttpContext.GetLocalResourceObject (VirtualPathUtility.ToAbsolute (this.AppRelativeVirtualPath),
341 protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
343 if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) || objType == null)
346 object localObject = GetLocalResourceObject (resourceKey);
347 if (localObject == null)
350 TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
351 if (converter == null || !converter.CanConvertFrom (localObject.GetType ()))
354 return converter.ConvertFrom (localObject);
357 internal override TemplateControl TemplateControlInternal {
362 [EditorBrowsable (EditorBrowsableState.Never)]
363 public static object ReadStringResource (Type t)
365 throw new NotSupportedException ();
368 [MonoTODO ("Not implemented, does nothing")]
369 [EditorBrowsable (EditorBrowsableState.Never)]
370 protected void SetStringResourcePointer (object stringResourcePointer,
371 int maxResourceOffset)
375 [MonoTODO ("Not implemented, does nothing")]
376 [EditorBrowsable (EditorBrowsableState.Never)]
377 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
378 int size, bool fAsciiOnly)
386 [WebSysDescription ("Raised when the user aborts a transaction.")]
387 public event EventHandler AbortTransaction {
388 add { Events.AddHandler (abortTransaction, value); }
389 remove { Events.RemoveHandler (abortTransaction, value); }
392 [WebSysDescription ("Raised when the user initiates a transaction.")]
393 public event EventHandler CommitTransaction {
394 add { Events.AddHandler (commitTransaction, value); }
395 remove { Events.RemoveHandler (commitTransaction, value); }
398 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
399 public event EventHandler Error {
400 add { Events.AddHandler (error, value); }
401 remove { Events.RemoveHandler (error, value); }
406 class SimpleTemplate : ITemplate
410 public SimpleTemplate (Type type)
415 public void InstantiateIn (Control control)
417 Control template = Activator.CreateInstance (type) as Control;
418 template.SetBindingContainer (false);
419 control.Controls.Add (template);
424 protected internal object Eval (string expression)
426 return DataBinder.Eval (Page.GetDataItem(), expression);
429 protected internal string Eval (string expression, string format)
431 return DataBinder.Eval (Page.GetDataItem(), expression, format);
434 protected internal object XPath (string xpathexpression)
436 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
439 protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
441 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
444 protected internal string XPath (string xpathexpression, string format)
446 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
449 protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
451 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
454 protected internal IEnumerable XPathSelect (string xpathexpression)
456 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
459 protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
461 return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
464 // IFilterResolutionService
466 [MonoTODO ("Not implemented")]
467 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
469 throw new NotImplementedException ();
472 [MonoTODO ("Not implemented")]
473 bool IFilterResolutionService.EvaluateFilter (string filterName)
475 throw new NotImplementedException ();