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 string _appRelativeVirtualPath;
81 protected TemplateControl ()
89 [EditorBrowsable (EditorBrowsableState.Never)]
93 protected virtual int AutoHandlers {
98 [EditorBrowsable (EditorBrowsableState.Never)]
99 protected virtual bool SupportAutoEvents {
104 public string AppRelativeVirtualPath {
105 get { return _appRelativeVirtualPath; }
106 set { _appRelativeVirtualPath = value; }
114 protected virtual void Construct ()
118 [MonoTODO ("Not implemented")]
119 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
126 internal void WireupAutomaticEvents ()
128 if (!SupportAutoEvents || !AutoEventWireup)
131 foreach (string methodName in methodNames) {
132 MethodInfo method = null;
134 for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
135 method = type.GetMethod (methodName, bflags);
142 if (method.DeclaringType != type) {
143 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
144 !method.IsFamilyAndAssembly && !method.IsFamily)
148 if (method.ReturnType != typeof (void))
151 ParameterInfo [] parms = method.GetParameters ();
152 int length = parms.Length;
153 bool noParams = (length == 0);
154 if (!noParams && (length != 2 ||
155 parms [0].ParameterType != typeof (object) ||
156 parms [1].ParameterType != typeof (EventArgs)))
159 int pos = methodName.IndexOf ("_");
160 string eventName = methodName.Substring (pos + 1);
161 EventInfo evt = type.GetEvent (eventName);
163 /* This should never happen */
168 NoParamsInvoker npi = new NoParamsInvoker (this, method);
169 evt.AddEventHandler (this, npi.FakeDelegate);
171 evt.AddEventHandler (this, Delegate.CreateDelegate (
173 typeof (EventHandler), this, method));
175 typeof (EventHandler), this, methodName));
181 [EditorBrowsable (EditorBrowsableState.Never)]
182 protected virtual void FrameworkInitialize ()
186 Type GetTypeFromControlPath (string virtualPath)
188 if (virtualPath == null)
189 throw new ArgumentNullException ("virtualPath");
191 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
192 string realpath = Context.Request.MapPath (vpath);
193 return UserControlParser.GetCompiledType (vpath, realpath, Context);
196 public Control LoadControl (string virtualPath)
199 if (virtualPath == null)
200 throw new ArgumentNullException ("virtualPath");
202 if (virtualPath == null)
203 throw new HttpException ("virtualPath is null");
205 Type type = GetTypeFromControlPath (virtualPath);
206 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
207 if (attrs != null && attrs.Length == 1) {
208 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
209 PartialCachingControl ctrl = new PartialCachingControl (type);
210 ctrl.VaryByParams = attr.VaryByParams;
211 ctrl.VaryByControls = attr.VaryByControls;
212 ctrl.VaryByCustom = attr.VaryByCustom;
216 object control = Activator.CreateInstance (type);
217 if (control is UserControl)
218 ((UserControl) control).InitializeAsUserControl (Page);
220 return (Control) control;
223 public ITemplate LoadTemplate (string virtualPath)
226 if (virtualPath == null)
227 throw new ArgumentNullException ("virtualPath");
229 if (virtualPath == null)
230 throw new HttpException ("virtualPath is null");
232 Type t = GetTypeFromControlPath (virtualPath);
233 return new SimpleTemplate (t);
236 protected virtual void OnAbortTransaction (EventArgs e)
238 EventHandler eh = Events [abortTransaction] as EventHandler;
243 protected virtual void OnCommitTransaction (EventArgs e)
245 EventHandler eh = Events [commitTransaction] as EventHandler;
250 protected virtual void OnError (EventArgs e)
252 EventHandler eh = Events [error] as EventHandler;
257 [MonoTODO ("Not implemented, always returns null")]
258 public Control ParseControl (string content)
261 throw new ArgumentNullException ("content");
266 [EditorBrowsable (EditorBrowsableState.Never)]
271 object ReadStringResource ()
273 throw new NotSupportedException ();
277 protected object GetGlobalResourceObject (string className, string resourceKey)
279 return HttpContext.GetGlobalResourceObject (className, resourceKey);
282 [MonoTODO ("Not implemented")]
283 protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
285 // FIXME: not sure how to implement that one yet
286 throw new NotSupportedException();
289 protected object GetLocalResourceObject (string resourceKey)
291 return HttpContext.GetLocalResourceObject (VirtualPathUtility.ToAbsolute (this.AppRelativeVirtualPath),
295 protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
297 // FIXME: not sure how to implement that one yet
298 throw new NotSupportedException();
302 [EditorBrowsable (EditorBrowsableState.Never)]
303 public static object ReadStringResource (Type t)
305 throw new NotSupportedException ();
308 [MonoTODO ("Not implemented, does nothing")]
309 [EditorBrowsable (EditorBrowsableState.Never)]
310 protected void SetStringResourcePointer (object stringResourcePointer,
311 int maxResourceOffset)
315 [MonoTODO ("Not implemented, does nothing")]
316 [EditorBrowsable (EditorBrowsableState.Never)]
317 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
318 int size, bool fAsciiOnly)
326 [WebSysDescription ("Raised when the user aborts a transaction.")]
327 public event EventHandler AbortTransaction {
328 add { Events.AddHandler (abortTransaction, value); }
329 remove { Events.RemoveHandler (abortTransaction, value); }
332 [WebSysDescription ("Raised when the user initiates a transaction.")]
333 public event EventHandler CommitTransaction {
334 add { Events.AddHandler (commitTransaction, value); }
335 remove { Events.RemoveHandler (commitTransaction, value); }
338 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
339 public event EventHandler Error {
340 add { Events.AddHandler (error, value); }
341 remove { Events.RemoveHandler (error, value); }
346 class SimpleTemplate : ITemplate
350 public SimpleTemplate (Type type)
355 public void InstantiateIn (Control control)
357 Control template = Activator.CreateInstance (type) as Control;
358 template.SetBindingContainer (false);
359 control.Controls.Add (template);
364 protected internal object Eval (string expression)
366 return DataBinder.Eval (Page.GetDataItem(), expression);
369 protected internal string Eval (string expression, string format)
371 return DataBinder.Eval (Page.GetDataItem(), expression, format);
374 protected internal object XPath (string xpathexpression)
376 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
379 protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
381 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
384 protected internal string XPath (string xpathexpression, string format)
386 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
389 protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
391 return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
394 protected internal IEnumerable XPathSelect (string xpathexpression)
396 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
399 protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
401 return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
404 // IFilterResolutionService
406 [MonoTODO ("Not implemented")]
407 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
409 throw new NotImplementedException ();
412 [MonoTODO ("Not implemented")]
413 bool IFilterResolutionService.EvaluateFilter (string filterName)
415 throw new NotImplementedException ();