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 int length = parms.Length;
93 bool noParams = (length == 0);
94 if (!noParams && (parms.Length != 2 ||
95 parms [0].ParameterType != typeof (object) ||
96 parms [1].ParameterType != typeof (EventArgs)))
99 string eventName = name.Substring (pos + 1);
100 EventInfo evt = type.GetEvent (eventName);
105 NoParamsInvoker npi = new NoParamsInvoker (this, method.Name);
106 evt.AddEventHandler (this, npi.FakeDelegate);
108 evt.AddEventHandler (this, Delegate.CreateDelegate (
109 typeof (EventHandler), this, method.Name));
114 [EditorBrowsable (EditorBrowsableState.Never)]
115 protected virtual void FrameworkInitialize ()
119 Type GetTypeFromControlPath (string virtualPath)
121 if (virtualPath == null)
122 throw new ArgumentNullException ("virtualPath");
124 return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
127 public Control LoadControl (string virtualPath)
129 object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
130 if (control is UserControl)
131 ((UserControl) control).InitializeAsUserControl (Page);
133 return (Control) control;
136 public ITemplate LoadTemplate (string virtualPath)
138 Type t = GetTypeFromControlPath (virtualPath);
139 return new SimpleTemplate (t);
142 protected virtual void OnAbortTransaction (EventArgs e)
144 EventHandler eh = Events [error] as EventHandler;
149 protected virtual void OnCommitTransaction (EventArgs e)
151 EventHandler eh = Events [commitTransaction] as EventHandler;
156 protected virtual void OnError (EventArgs e)
158 EventHandler eh = Events [abortTransaction] as EventHandler;
164 public Control ParseControl (string content)
170 [EditorBrowsable (EditorBrowsableState.Never)]
171 public static object ReadStringResource (Type t)
177 [EditorBrowsable (EditorBrowsableState.Never)]
178 protected void SetStringResourcePointer (object stringResourcePointer,
179 int maxResourceOffset)
184 [EditorBrowsable (EditorBrowsableState.Never)]
185 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
186 int size, bool fAsciiOnly)
194 [WebSysDescription ("Raised when the user aborts a transaction.")]
195 public event EventHandler AbortTransaction {
196 add { Events.AddHandler (abortTransaction, value); }
197 remove { Events.RemoveHandler (abortTransaction, value); }
200 [WebSysDescription ("Raised when the user initiates a transaction.")]
201 public event EventHandler CommitTransaction {
202 add { Events.AddHandler (commitTransaction, value); }
203 remove { Events.RemoveHandler (commitTransaction, value); }
206 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
207 public event EventHandler Error {
208 add { Events.AddHandler (error, value); }
209 remove { Events.RemoveHandler (error, value); }
214 class SimpleTemplate : ITemplate
218 public SimpleTemplate (Type type)
223 public void InstantiateIn (Control control)
225 Control template = Activator.CreateInstance (type) as Control;
226 template.SetBindingContainer (false);
227 control.Controls.Add (template);
231 delegate void NoParamsDelegate ();
232 class NoParamsInvoker
235 NoParamsDelegate real;
237 public NoParamsInvoker (object o, string method)
239 real = (NoParamsDelegate) Delegate.CreateDelegate (
240 typeof (NoParamsDelegate), o, method);
241 faked = new EventHandler (InvokeNoParams);
244 void InvokeNoParams (object o, EventArgs args)
249 public EventHandler FakeDelegate {
250 get { return faked; }