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 object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
173 if (control is UserControl)
174 ((UserControl) control).InitializeAsUserControl (Page);
176 return (Control) control;
179 public ITemplate LoadTemplate (string virtualPath)
182 if (virtualPath == null)
183 throw new ArgumentNullException ("virtualPath");
185 if (virtualPath == null)
186 throw new HttpException ("virtualPath is null");
188 Type t = GetTypeFromControlPath (virtualPath);
189 return new SimpleTemplate (t);
192 protected virtual void OnAbortTransaction (EventArgs e)
194 EventHandler eh = Events [abortTransaction] as EventHandler;
199 protected virtual void OnCommitTransaction (EventArgs e)
201 EventHandler eh = Events [commitTransaction] as EventHandler;
206 protected virtual void OnError (EventArgs e)
208 EventHandler eh = Events [error] as EventHandler;
214 public Control ParseControl (string content)
217 throw new ArgumentNullException ("content");
223 [EditorBrowsable (EditorBrowsableState.Never)]
224 public static object ReadStringResource (Type t)
227 throw new ArgumentNullException ("t");
233 [EditorBrowsable (EditorBrowsableState.Never)]
234 protected void SetStringResourcePointer (object stringResourcePointer,
235 int maxResourceOffset)
240 [EditorBrowsable (EditorBrowsableState.Never)]
241 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
242 int size, bool fAsciiOnly)
250 [WebSysDescription ("Raised when the user aborts a transaction.")]
251 public event EventHandler AbortTransaction {
252 add { Events.AddHandler (abortTransaction, value); }
253 remove { Events.RemoveHandler (abortTransaction, value); }
256 [WebSysDescription ("Raised when the user initiates a transaction.")]
257 public event EventHandler CommitTransaction {
258 add { Events.AddHandler (commitTransaction, value); }
259 remove { Events.RemoveHandler (commitTransaction, value); }
262 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
263 public event EventHandler Error {
264 add { Events.AddHandler (error, value); }
265 remove { Events.RemoveHandler (error, value); }
270 class SimpleTemplate : ITemplate
274 public SimpleTemplate (Type type)
279 public void InstantiateIn (Control control)
281 Control template = Activator.CreateInstance (type) as Control;
282 template.SetBindingContainer (false);
283 control.Controls.Add (template);
291 internal void PushDataItemContext (object o)
293 if (dataItemCtx == null)
294 dataItemCtx = new Stack ();
296 dataItemCtx.Push (o);
299 internal void PopDataItemContext ()
301 if (dataItemCtx == null)
302 throw new InvalidOperationException ();
307 internal object CurrentDataItem {
309 if (dataItemCtx == null || dataItemCtx.Count == 0)
310 throw new InvalidOperationException ("No data item");
312 return dataItemCtx.Peek ();
316 protected object Eval (string expression)
318 return DataBinder.Eval (CurrentDataItem, expression);
321 protected object Eval (string expression, string format)
323 return DataBinder.Eval (CurrentDataItem, expression, format);
326 protected object XPath (string xpathexpression)
328 return XPathBinder.Eval (CurrentDataItem, xpathexpression);
331 protected object XPath (string xpathexpression, string format)
333 return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
336 protected IEnumerable XPathSelect (string xpathexpression)
338 return XPathBinder.Select (CurrentDataItem, xpathexpression);
341 // IFilterResolutionService
344 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
346 throw new NotImplementedException ();
350 bool IFilterResolutionService.EvaluateFilter (string filterName)
352 throw new NotImplementedException ();