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)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.ComponentModel;
36 using System.Reflection;
38 using System.Web.Compilation;
39 using System.Web.Util;
41 namespace System.Web.UI {
43 public abstract class TemplateControl : Control, INamingContainer
45 static object abortTransaction = new object ();
46 static object commitTransaction = new object ();
47 static object error = new object ();
48 static string [] methodNames = { "Page_Init",
55 "Page_AbortTransaction",
56 "Page_CommitTransaction" };
58 const BindingFlags bflags = BindingFlags.Public |
59 BindingFlags.NonPublic |
60 BindingFlags.Instance;
63 protected TemplateControl ()
71 [EditorBrowsable (EditorBrowsableState.Never)]
72 protected virtual int AutoHandlers {
77 [EditorBrowsable (EditorBrowsableState.Never)]
78 protected virtual bool SupportAutoEvents {
86 protected virtual void Construct ()
91 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
98 internal void WireupAutomaticEvents ()
100 if (!SupportAutoEvents || !AutoEventWireup)
103 Type type = GetType ();
104 foreach (string methodName in methodNames) {
105 MethodInfo method = type.GetMethod (methodName, bflags);
109 if (method.ReturnType != typeof (void))
112 ParameterInfo [] parms = method.GetParameters ();
113 int length = parms.Length;
114 bool noParams = (length == 0);
115 if (!noParams && (length != 2 ||
116 parms [0].ParameterType != typeof (object) ||
117 parms [1].ParameterType != typeof (EventArgs)))
120 int pos = methodName.IndexOf ("_");
121 string eventName = methodName.Substring (pos + 1);
122 EventInfo evt = type.GetEvent (eventName);
124 /* This should never happen */
129 NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
130 evt.AddEventHandler (this, npi.FakeDelegate);
132 evt.AddEventHandler (this, Delegate.CreateDelegate (
133 typeof (EventHandler), this, methodName));
138 [EditorBrowsable (EditorBrowsableState.Never)]
139 protected virtual void FrameworkInitialize ()
143 Type GetTypeFromControlPath (string virtualPath)
145 if (virtualPath == null)
146 throw new ArgumentNullException ("virtualPath");
148 string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
149 string realpath = Context.Request.MapPath (vpath);
150 return UserControlParser.GetCompiledType (vpath, realpath, Context);
153 public Control LoadControl (string virtualPath)
155 object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
156 if (control is UserControl)
157 ((UserControl) control).InitializeAsUserControl (Page);
159 return (Control) control;
162 public ITemplate LoadTemplate (string virtualPath)
164 Type t = GetTypeFromControlPath (virtualPath);
165 return new SimpleTemplate (t);
168 protected virtual void OnAbortTransaction (EventArgs e)
170 EventHandler eh = Events [abortTransaction] as EventHandler;
175 protected virtual void OnCommitTransaction (EventArgs e)
177 EventHandler eh = Events [commitTransaction] as EventHandler;
182 protected virtual void OnError (EventArgs e)
184 EventHandler eh = Events [error] as EventHandler;
190 public Control ParseControl (string content)
196 [EditorBrowsable (EditorBrowsableState.Never)]
197 public static object ReadStringResource (Type t)
203 [EditorBrowsable (EditorBrowsableState.Never)]
204 protected void SetStringResourcePointer (object stringResourcePointer,
205 int maxResourceOffset)
210 [EditorBrowsable (EditorBrowsableState.Never)]
211 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
212 int size, bool fAsciiOnly)
220 [WebSysDescription ("Raised when the user aborts a transaction.")]
221 public event EventHandler AbortTransaction {
222 add { Events.AddHandler (abortTransaction, value); }
223 remove { Events.RemoveHandler (abortTransaction, value); }
226 [WebSysDescription ("Raised when the user initiates a transaction.")]
227 public event EventHandler CommitTransaction {
228 add { Events.AddHandler (commitTransaction, value); }
229 remove { Events.RemoveHandler (commitTransaction, value); }
232 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
233 public event EventHandler Error {
234 add { Events.AddHandler (error, value); }
235 remove { Events.RemoveHandler (error, value); }
240 class SimpleTemplate : ITemplate
244 public SimpleTemplate (Type type)
249 public void InstantiateIn (Control control)
251 Control template = Activator.CreateInstance (type) as Control;
252 template.SetBindingContainer (false);
253 control.Controls.Add (template);