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)]
79 protected virtual int AutoHandlers {
84 [EditorBrowsable (EditorBrowsableState.Never)]
85 protected virtual bool SupportAutoEvents {
93 protected virtual void Construct ()
98 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
105 internal void WireupAutomaticEvents ()
107 if (!SupportAutoEvents || !AutoEventWireup)
110 Type type = GetType ();
111 foreach (string methodName in methodNames) {
112 MethodInfo method = type.GetMethod (methodName, bflags);
116 if (method.DeclaringType != type) {
117 if (!method.IsPublic && !method.IsFamilyOrAssembly &&
118 !method.IsFamilyAndAssembly && !method.IsFamily)
122 if (method.ReturnType != typeof (void))
125 ParameterInfo [] parms = method.GetParameters ();
126 int length = parms.Length;
127 bool noParams = (length == 0);
128 if (!noParams && (length != 2 ||
129 parms [0].ParameterType != typeof (object) ||
130 parms [1].ParameterType != typeof (EventArgs)))
133 int pos = methodName.IndexOf ("_");
134 string eventName = methodName.Substring (pos + 1);
135 EventInfo evt = type.GetEvent (eventName);
137 /* This should never happen */
142 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
143 evt.AddEventHandler (this, npi.FakeDelegate);
145 evt.AddEventHandler (this, Delegate.CreateDelegate (
146 typeof (EventHandler), this, methodName));
151 [EditorBrowsable (EditorBrowsableState.Never)]
152 protected virtual void FrameworkInitialize ()
156 Type GetTypeFromControlPath (string virtualPath)
158 if (virtualPath == null)
159 throw new ArgumentNullException ("virtualPath");
161 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
162 string realpath = Context.Request.MapPath (vpath);
163 return UserControlParser.GetCompiledType (vpath, realpath, Context);
166 public Control LoadControl (string virtualPath)
169 if (virtualPath == null)
170 throw new ArgumentNullException ("virtualPath");
172 if (virtualPath == null)
173 throw new HttpException ("virtualPath is null");
175 Type type = GetTypeFromControlPath (virtualPath);
176 object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
177 if (attrs != null && attrs.Length == 1) {
178 PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
179 PartialCachingControl ctrl = new PartialCachingControl (type);
180 ctrl.VaryByParams = attr.VaryByParams;
181 ctrl.VaryByControls = attr.VaryByControls;
182 ctrl.VaryByCustom = attr.VaryByCustom;
186 object control = Activator.CreateInstance (type);
187 if (control is UserControl)
188 ((UserControl) control).InitializeAsUserControl (Page);
190 return (Control) control;
193 public ITemplate LoadTemplate (string virtualPath)
196 if (virtualPath == null)
197 throw new ArgumentNullException ("virtualPath");
199 if (virtualPath == null)
200 throw new HttpException ("virtualPath is null");
202 Type t = GetTypeFromControlPath (virtualPath);
203 return new SimpleTemplate (t);
206 protected virtual void OnAbortTransaction (EventArgs e)
208 EventHandler eh = Events [abortTransaction] as EventHandler;
213 protected virtual void OnCommitTransaction (EventArgs e)
215 EventHandler eh = Events [commitTransaction] as EventHandler;
220 protected virtual void OnError (EventArgs e)
222 EventHandler eh = Events [error] as EventHandler;
228 public Control ParseControl (string content)
231 throw new ArgumentNullException ("content");
236 [EditorBrowsable (EditorBrowsableState.Never)]
241 object ReadStringResource ()
243 throw new NotSupportedException ();
246 [EditorBrowsable (EditorBrowsableState.Never)]
247 public static object ReadStringResource (Type t)
249 throw new NotSupportedException ();
253 [EditorBrowsable (EditorBrowsableState.Never)]
254 protected void SetStringResourcePointer (object stringResourcePointer,
255 int maxResourceOffset)
260 [EditorBrowsable (EditorBrowsableState.Never)]
261 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
262 int size, bool fAsciiOnly)
270 [WebSysDescription ("Raised when the user aborts a transaction.")]
271 public event EventHandler AbortTransaction {
272 add { Events.AddHandler (abortTransaction, value); }
273 remove { Events.RemoveHandler (abortTransaction, value); }
276 [WebSysDescription ("Raised when the user initiates a transaction.")]
277 public event EventHandler CommitTransaction {
278 add { Events.AddHandler (commitTransaction, value); }
279 remove { Events.RemoveHandler (commitTransaction, value); }
282 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
283 public event EventHandler Error {
284 add { Events.AddHandler (error, value); }
285 remove { Events.RemoveHandler (error, value); }
290 class SimpleTemplate : ITemplate
294 public SimpleTemplate (Type type)
299 public void InstantiateIn (Control control)
301 Control template = Activator.CreateInstance (type) as Control;
302 template.SetBindingContainer (false);
303 control.Controls.Add (template);
308 protected object Eval (string expression)
310 return DataBinder.Eval (Page.GetDataItem(), expression);
313 protected string Eval (string expression, string format)
315 return DataBinder.Eval (Page.GetDataItem(), expression, format);
318 protected object XPath (string xpathexpression)
320 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
323 protected string XPath (string xpathexpression, string format)
325 return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
328 protected IEnumerable XPathSelect (string xpathexpression)
330 return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
333 // IFilterResolutionService
336 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
338 throw new NotImplementedException ();
342 bool IFilterResolutionService.EvaluateFilter (string filterName)
344 throw new NotImplementedException ();