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 using System.Collections;
14 using System.ComponentModel;
15 using System.Reflection;
16 using System.Web.Compilation;
17 using System.Web.Util;
19 namespace System.Web.UI {
21 public abstract class TemplateControl : Control, INamingContainer
23 static object abortTransaction = new object ();
24 static object commitTransaction = new object ();
25 static object error = new object ();
26 static string [] methodNames = { "Page_Init",
33 const BindingFlags bflags = BindingFlags.Public |
34 BindingFlags.NonPublic |
35 BindingFlags.Instance;
38 protected TemplateControl ()
47 protected virtual int AutoHandlers {
52 protected virtual bool SupportAutoEvents {
60 protected virtual void Construct ()
65 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
72 internal void WireupAutomaticEvents ()
74 if (!SupportAutoEvents || !AutoEventWireup)
77 Type type = GetType ();
78 foreach (MethodInfo method in type.GetMethods (bflags)) {
79 int pos = Array.IndexOf (methodNames, method.Name);
83 string name = methodNames [pos];
84 pos = name.IndexOf ("_");
85 if (pos == -1 || pos + 1 == name.Length)
88 if (method.ReturnType != typeof (void))
91 ParameterInfo [] parms = method.GetParameters ();
92 if (parms.Length != 2 ||
93 parms [0].ParameterType != typeof (object) ||
94 parms [1].ParameterType != typeof (EventArgs))
97 string eventName = name.Substring (pos + 1);
98 EventInfo evt = type.GetEvent (eventName);
102 evt.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), method));
106 [EditorBrowsable (EditorBrowsableState.Never)]
107 protected virtual void FrameworkInitialize ()
111 Type GetTypeFromControlPath (string virtualPath)
113 if (virtualPath == null)
114 throw new ArgumentNullException ("virtualPath");
116 return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
119 public Control LoadControl (string virtualPath)
121 object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
122 if (control is UserControl)
123 ((UserControl) control).InitializeAsUserControl (Page);
125 return (Control) control;
128 public ITemplate LoadTemplate (string virtualPath)
130 Type t = GetTypeFromControlPath (virtualPath);
131 return new SimpleTemplate (t);
134 protected virtual void OnAbortTransaction (EventArgs e)
136 EventHandler eh = Events [error] as EventHandler;
141 protected virtual void OnCommitTransaction (EventArgs e)
143 EventHandler eh = Events [commitTransaction] as EventHandler;
148 protected virtual void OnError (EventArgs e)
150 EventHandler eh = Events [abortTransaction] as EventHandler;
156 public Control ParseControl (string content)
162 [EditorBrowsable (EditorBrowsableState.Never)]
163 public static object ReadStringResource (Type t)
169 [EditorBrowsable (EditorBrowsableState.Never)]
170 protected void SetStringResourcePointer (object stringResourcePointer,
171 int maxResourceOffset)
176 [EditorBrowsable (EditorBrowsableState.Never)]
177 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
178 int size, bool fAsciiOnly)
186 [WebSysDescription ("Raised when the user aborts a transaction.")]
187 public event EventHandler AbortTransaction {
188 add { Events.AddHandler (abortTransaction, value); }
189 remove { Events.RemoveHandler (abortTransaction, value); }
192 [WebSysDescription ("Raised when the user initiates a transaction.")]
193 public event EventHandler CommitTransaction {
194 add { Events.AddHandler (commitTransaction, value); }
195 remove { Events.RemoveHandler (commitTransaction, value); }
198 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
199 public event EventHandler Error {
200 add { Events.AddHandler (error, value); }
201 remove { Events.RemoveHandler (error, value); }
206 class SimpleTemplate : ITemplate
210 public SimpleTemplate (Type type)
215 public void InstantiateIn (Control control)
217 Control template = Activator.CreateInstance (type) as Control;
218 template.SetBindingContainer (false);
219 control.Controls.Add (template);