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",
57 "Page_PreRenderComplete",
58 "Page_SaveStateComplete",
67 "Page_AbortTransaction",
68 "Page_CommitTransaction"};
70 const BindingFlags bflags = BindingFlags.Public |
71 BindingFlags.NonPublic |
72 BindingFlags.Instance;
75 protected TemplateControl ()
83 [EditorBrowsable (EditorBrowsableState.Never)]
87 protected virtual int AutoHandlers {
92 [EditorBrowsable (EditorBrowsableState.Never)]
93 protected virtual bool SupportAutoEvents {
101 protected virtual void Construct ()
106 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
113 internal void WireupAutomaticEvents ()
115 if (!SupportAutoEvents || !AutoEventWireup)
118 Type type = GetType ();
119 foreach (string methodName in methodNames) {
120 MethodInfo method = type.GetMethod (methodName, bflags);
124 if (method.DeclaringType != type) {
125 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
126 !method.IsFamilyAndAssembly && !method.IsFamily)
130 if (method.ReturnType != typeof (void))
133 ParameterInfo [] parms = method.GetParameters ();
134 int length = parms.Length;
135 bool noParams = (length == 0);
136 if (!noParams && (length != 2 ||
137 parms [0].ParameterType != typeof (object) ||
138 parms [1].ParameterType != typeof (EventArgs)))
141 int pos = methodName.IndexOf ("_");
142 string eventName = methodName.Substring (pos + 1);
143 EventInfo evt = type.GetEvent (eventName);
145 /* This should never happen */
150 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
151 evt.AddEventHandler (this, npi.FakeDelegate);
153 evt.AddEventHandler (this, Delegate.CreateDelegate (
154 typeof (EventHandler), this, methodName));
159 [EditorBrowsable (EditorBrowsableState.Never)]
160 protected virtual void FrameworkInitialize ()
164 Type GetTypeFromControlPath (string virtualPath)
166 if (virtualPath == null)
167 throw new ArgumentNullException ("virtualPath");
169 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
170 string realpath = Context.Request.MapPath (vpath);
171 return UserControlParser.GetCompiledType (vpath, realpath, Context);
174 public Control LoadControl (string virtualPath)
177 if (virtualPath == null)
178 throw new ArgumentNullException ("virtualPath");
180 if (virtualPath == null)
181 throw new HttpException ("virtualPath is null");
183 Type type = GetTypeFromControlPath (virtualPath);
184 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
185 if (attrs != null && attrs.Length == 1) {
186 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
187 PartialCachingControl ctrl = new PartialCachingControl (type);
188 ctrl.VaryByParams = attr.VaryByParams;
189 ctrl.VaryByControls = attr.VaryByControls;
190 ctrl.VaryByCustom = attr.VaryByCustom;
194 object control = Activator.CreateInstance (type);
195 if (control is UserControl)
196 ((UserControl) control).InitializeAsUserControl (Page);
198 return (Control) control;
201 public ITemplate LoadTemplate (string virtualPath)
204 if (virtualPath == null)
205 throw new ArgumentNullException ("virtualPath");
207 if (virtualPath == null)
208 throw new HttpException ("virtualPath is null");
210 Type t = GetTypeFromControlPath (virtualPath);
211 return new SimpleTemplate (t);
214 protected virtual void OnAbortTransaction (EventArgs e)
216 EventHandler eh = Events [abortTransaction] as EventHandler;
221 protected virtual void OnCommitTransaction (EventArgs e)
223 EventHandler eh = Events [commitTransaction] as EventHandler;
228 protected virtual void OnError (EventArgs e)
230 EventHandler eh = Events [error] as EventHandler;
236 public Control ParseControl (string content)
239 throw new ArgumentNullException ("content");
244 [EditorBrowsable (EditorBrowsableState.Never)]
249 object ReadStringResource ()
251 throw new NotSupportedException ();
255 protected object GetGlobalResourceObject (string className, string resourceKey)
257 return HttpContext.GetGlobalResourceObject (className, resourceKey);
261 protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
263 // FIXME: not sure how to implement that one yet
264 throw new NotSupportedException();
267 protected object GetLocalResourceObject (string resourceKey)
269 return HttpContext.GetLocalResourceObject (Context.Request.CurrentExecutionFilePath, resourceKey);
272 protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
274 // FIXME: not sure how to implement that one yet
275 throw new NotSupportedException();
279 [EditorBrowsable (EditorBrowsableState.Never)]
280 public static object ReadStringResource (Type t)
282 throw new NotSupportedException ();
286 [EditorBrowsable (EditorBrowsableState.Never)]
287 protected void SetStringResourcePointer (object stringResourcePointer,
288 int maxResourceOffset)
293 [EditorBrowsable (EditorBrowsableState.Never)]
294 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
295 int size, bool fAsciiOnly)
303 [WebSysDescription ("Raised when the user aborts a transaction.")]
304 public event EventHandler AbortTransaction {
305 add { Events.AddHandler (abortTransaction, value); }
306 remove { Events.RemoveHandler (abortTransaction, value); }
309 [WebSysDescription ("Raised when the user initiates a transaction.")]
310 public event EventHandler CommitTransaction {
311 add { Events.AddHandler (commitTransaction, value); }
312 remove { Events.RemoveHandler (commitTransaction, value); }
315 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
316 public event EventHandler Error {
317 add { Events.AddHandler (error, value); }
318 remove { Events.RemoveHandler (error, value); }
323 class SimpleTemplate : ITemplate
327 public SimpleTemplate (Type type)
332 public void InstantiateIn (Control control)
334 Control template = Activator.CreateInstance (type) as Control;
335 template.SetBindingContainer (false);
336 control.Controls.Add (template);
341 protected object Eval (string expression)
343 return DataBinder.Eval (Page.GetDataItem(), expression);
346 protected string Eval (string expression, string format)
348 return DataBinder.Eval (Page.GetDataItem(), expression, format);
351 protected object XPath (string xpathexpression)
353 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
356 protected string XPath (string xpathexpression, string format)
358 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
361 protected IEnumerable XPathSelect (string xpathexpression)
363 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
366 // IFilterResolutionService
369 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
371 throw new NotImplementedException ();
375 bool IFilterResolutionService.EvaluateFilter (string filterName)
377 throw new NotImplementedException ();