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");
234 [EditorBrowsable (EditorBrowsableState.Never)]
235 public static object ReadStringResource (Type t)
238 throw new ArgumentNullException ("t");
244 [EditorBrowsable (EditorBrowsableState.Never)]
245 protected void SetStringResourcePointer (object stringResourcePointer,
246 int maxResourceOffset)
251 [EditorBrowsable (EditorBrowsableState.Never)]
252 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
253 int size, bool fAsciiOnly)
261 [WebSysDescription ("Raised when the user aborts a transaction.")]
262 public event EventHandler AbortTransaction {
263 add { Events.AddHandler (abortTransaction, value); }
264 remove { Events.RemoveHandler (abortTransaction, value); }
267 [WebSysDescription ("Raised when the user initiates a transaction.")]
268 public event EventHandler CommitTransaction {
269 add { Events.AddHandler (commitTransaction, value); }
270 remove { Events.RemoveHandler (commitTransaction, value); }
273 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
274 public event EventHandler Error {
275 add { Events.AddHandler (error, value); }
276 remove { Events.RemoveHandler (error, value); }
281 class SimpleTemplate : ITemplate
285 public SimpleTemplate (Type type)
290 public void InstantiateIn (Control control)
292 Control template = Activator.CreateInstance (type) as Control;
293 template.SetBindingContainer (false);
294 control.Controls.Add (template);
302 internal void PushDataItemContext (object o)
304 if (dataItemCtx == null)
305 dataItemCtx = new Stack ();
307 dataItemCtx.Push (o);
310 internal void PopDataItemContext ()
312 if (dataItemCtx == null)
313 throw new InvalidOperationException ();
318 internal object CurrentDataItem {
320 if (dataItemCtx == null || dataItemCtx.Count == 0)
321 throw new InvalidOperationException ("No data item");
323 return dataItemCtx.Peek ();
327 protected object Eval (string expression)
329 return DataBinder.Eval (CurrentDataItem, expression);
332 protected object Eval (string expression, string format)
334 return DataBinder.Eval (CurrentDataItem, expression, format);
337 protected object XPath (string xpathexpression)
339 return XPathBinder.Eval (CurrentDataItem, xpathexpression);
342 protected object XPath (string xpathexpression, string format)
344 return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
347 protected IEnumerable XPathSelect (string xpathexpression)
349 return XPathBinder.Select (CurrentDataItem, xpathexpression);
352 // IFilterResolutionService
355 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
357 throw new NotImplementedException ();
361 bool IFilterResolutionService.EvaluateFilter (string filterName)
363 throw new NotImplementedException ();