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",
59 "Page_AbortTransaction",
60 "Page_CommitTransaction" };
62 const BindingFlags bflags = BindingFlags.Public |
63 BindingFlags.NonPublic |
64 BindingFlags.Instance;
67 protected TemplateControl ()
75 [EditorBrowsable (EditorBrowsableState.Never)]
76 protected virtual int AutoHandlers {
81 [EditorBrowsable (EditorBrowsableState.Never)]
82 protected virtual bool SupportAutoEvents {
90 protected virtual void Construct ()
95 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
102 internal void WireupAutomaticEvents ()
104 if (!SupportAutoEvents || !AutoEventWireup)
107 Type type = GetType ();
108 foreach (string methodName in methodNames) {
109 MethodInfo method = type.GetMethod (methodName, bflags);
113 if (method.DeclaringType != type) {
114 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
115 !method.IsFamilyAndAssembly && !method.IsFamily)
119 if (method.ReturnType != typeof (void))
122 ParameterInfo [] parms = method.GetParameters ();
123 int length = parms.Length;
124 bool noParams = (length == 0);
125 if (!noParams && (length != 2 ||
126 parms [0].ParameterType != typeof (object) ||
127 parms [1].ParameterType != typeof (EventArgs)))
130 int pos = methodName.IndexOf ("_");
131 string eventName = methodName.Substring (pos + 1);
132 EventInfo evt = type.GetEvent (eventName);
134 /* This should never happen */
139 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
140 evt.AddEventHandler (this, npi.FakeDelegate);
142 evt.AddEventHandler (this, Delegate.CreateDelegate (
143 typeof (EventHandler), this, methodName));
148 [EditorBrowsable (EditorBrowsableState.Never)]
149 protected virtual void FrameworkInitialize ()
153 Type GetTypeFromControlPath (string virtualPath)
155 if (virtualPath == null)
156 throw new ArgumentNullException ("virtualPath");
158 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
159 string realpath = Context.Request.MapPath (vpath);
160 return UserControlParser.GetCompiledType (vpath, realpath, Context);
163 public Control LoadControl (string virtualPath)
166 if (virtualPath == null)
167 throw new ArgumentNullException ("virtualPath");
169 if (virtualPath == null)
170 throw new HttpException ("virtualPath is null");
172 Type type = GetTypeFromControlPath (virtualPath);
173 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
174 if (attrs != null && attrs.Length == 1) {
175 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
176 PartialCachingControl ctrl = new PartialCachingControl (type);
177 ctrl.VaryByParams = attr.VaryByParams;
178 ctrl.VaryByControls = attr.VaryByControls;
179 ctrl.VaryByCustom = attr.VaryByCustom;
183 object control = Activator.CreateInstance (type);
184 if (control is UserControl)
185 ((UserControl) control).InitializeAsUserControl (Page);
187 return (Control) control;
190 public ITemplate LoadTemplate (string virtualPath)
193 if (virtualPath == null)
194 throw new ArgumentNullException ("virtualPath");
196 if (virtualPath == null)
197 throw new HttpException ("virtualPath is null");
199 Type t = GetTypeFromControlPath (virtualPath);
200 return new SimpleTemplate (t);
203 protected virtual void OnAbortTransaction (EventArgs e)
205 EventHandler eh = Events [abortTransaction] as EventHandler;
210 protected virtual void OnCommitTransaction (EventArgs e)
212 EventHandler eh = Events [commitTransaction] as EventHandler;
217 protected virtual void OnError (EventArgs e)
219 EventHandler eh = Events [error] as EventHandler;
225 public Control ParseControl (string content)
228 throw new ArgumentNullException ("content");
233 [EditorBrowsable (EditorBrowsableState.Never)]
234 public static object ReadStringResource ()
236 throw new NotSupportedException ();
239 [EditorBrowsable (EditorBrowsableState.Never)]
240 public static object ReadStringResource (Type t)
242 throw new NotSupportedException ();
246 [EditorBrowsable (EditorBrowsableState.Never)]
247 protected void SetStringResourcePointer (object stringResourcePointer,
248 int maxResourceOffset)
253 [EditorBrowsable (EditorBrowsableState.Never)]
254 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
255 int size, bool fAsciiOnly)
263 [WebSysDescription ("Raised when the user aborts a transaction.")]
264 public event EventHandler AbortTransaction {
265 add { Events.AddHandler (abortTransaction, value); }
266 remove { Events.RemoveHandler (abortTransaction, value); }
269 [WebSysDescription ("Raised when the user initiates a transaction.")]
270 public event EventHandler CommitTransaction {
271 add { Events.AddHandler (commitTransaction, value); }
272 remove { Events.RemoveHandler (commitTransaction, value); }
275 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
276 public event EventHandler Error {
277 add { Events.AddHandler (error, value); }
278 remove { Events.RemoveHandler (error, value); }
283 class SimpleTemplate : ITemplate
287 public SimpleTemplate (Type type)
292 public void InstantiateIn (Control control)
294 Control template = Activator.CreateInstance (type) as Control;
295 template.SetBindingContainer (false);
296 control.Controls.Add (template);
304 internal void PushDataItemContext (object o)
306 if (dataItemCtx == null)
307 dataItemCtx = new Stack ();
309 dataItemCtx.Push (o);
312 internal void PopDataItemContext ()
314 if (dataItemCtx == null)
315 throw new InvalidOperationException ();
320 internal object CurrentDataItem {
322 if (dataItemCtx == null || dataItemCtx.Count == 0)
323 throw new InvalidOperationException ("No data item");
325 return dataItemCtx.Peek ();
329 protected object Eval (string expression)
331 return DataBinder.Eval (CurrentDataItem, expression);
334 protected object Eval (string expression, string format)
336 return DataBinder.Eval (CurrentDataItem, expression, format);
339 protected object XPath (string xpathexpression)
341 return XPathBinder.Eval (CurrentDataItem, xpathexpression);
344 protected object XPath (string xpathexpression, string format)
346 return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
349 protected IEnumerable XPathSelect (string xpathexpression)
351 return XPathBinder.Select (CurrentDataItem, xpathexpression);
354 // IFilterResolutionService
357 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
359 throw new NotImplementedException ();
363 bool IFilterResolutionService.EvaluateFilter (string filterName)
365 throw new NotImplementedException ();