2
0

TemplateControl.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.Web.UI.TemplateControl.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Reflection;
  15. using System.Web.Compilation;
  16. using System.Web.Util;
  17. namespace System.Web.UI {
  18. public abstract class TemplateControl : Control, INamingContainer
  19. {
  20. static object abortTransaction = new object ();
  21. static object commitTransaction = new object ();
  22. static object error = new object ();
  23. static string [] methodNames = { "Page_Init",
  24. "Page_Load",
  25. "Page_DataBind",
  26. "Page_PreRender",
  27. "Page_Dispose",
  28. "Page_Error" };
  29. const BindingFlags bflags = BindingFlags.Public |
  30. BindingFlags.NonPublic |
  31. BindingFlags.Instance;
  32. #region Constructor
  33. protected TemplateControl ()
  34. {
  35. Construct ();
  36. }
  37. #endregion
  38. #region Properties
  39. protected virtual int AutoHandlers {
  40. get { return 0; }
  41. set { }
  42. }
  43. protected virtual bool SupportAutoEvents {
  44. get { return true; }
  45. }
  46. #endregion
  47. #region Methods
  48. protected virtual void Construct ()
  49. {
  50. }
  51. [MonoTODO]
  52. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  53. int size,
  54. bool fAsciiOnly)
  55. {
  56. return null;
  57. }
  58. internal void WireupAutomaticEvents ()
  59. {
  60. if (!SupportAutoEvents || !AutoEventWireup)
  61. return;
  62. Type type = GetType ();
  63. foreach (MethodInfo method in type.GetMethods (bflags)) {
  64. int pos = Array.IndexOf (methodNames, method.Name);
  65. if (pos == -1)
  66. continue;
  67. string name = methodNames [pos];
  68. pos = name.IndexOf ("_");
  69. if (pos == -1 || pos + 1 == name.Length)
  70. continue;
  71. if (method.ReturnType != typeof (void))
  72. continue;
  73. ParameterInfo [] parms = method.GetParameters ();
  74. int length = parms.Length;
  75. bool noParams = (length == 0);
  76. if (!noParams && (parms.Length != 2 ||
  77. parms [0].ParameterType != typeof (object) ||
  78. parms [1].ParameterType != typeof (EventArgs)))
  79. continue;
  80. string eventName = name.Substring (pos + 1);
  81. EventInfo evt = type.GetEvent (eventName);
  82. if (evt == null)
  83. continue;
  84. if (noParams) {
  85. NoParamsInvoker npi = new NoParamsInvoker (this, method.Name);
  86. evt.AddEventHandler (this, npi.FakeDelegate);
  87. } else {
  88. evt.AddEventHandler (this, Delegate.CreateDelegate (
  89. typeof (EventHandler), this, method.Name));
  90. }
  91. }
  92. }
  93. [EditorBrowsable (EditorBrowsableState.Never)]
  94. protected virtual void FrameworkInitialize ()
  95. {
  96. }
  97. Type GetTypeFromControlPath (string virtualPath)
  98. {
  99. if (virtualPath == null)
  100. throw new ArgumentNullException ("virtualPath");
  101. return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
  102. }
  103. public Control LoadControl (string virtualPath)
  104. {
  105. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  106. if (control is UserControl)
  107. ((UserControl) control).InitializeAsUserControl (Page);
  108. return (Control) control;
  109. }
  110. public ITemplate LoadTemplate (string virtualPath)
  111. {
  112. Type t = GetTypeFromControlPath (virtualPath);
  113. return new SimpleTemplate (t);
  114. }
  115. protected virtual void OnAbortTransaction (EventArgs e)
  116. {
  117. EventHandler eh = Events [error] as EventHandler;
  118. if (eh != null)
  119. eh (this, e);
  120. }
  121. protected virtual void OnCommitTransaction (EventArgs e)
  122. {
  123. EventHandler eh = Events [commitTransaction] as EventHandler;
  124. if (eh != null)
  125. eh (this, e);
  126. }
  127. protected virtual void OnError (EventArgs e)
  128. {
  129. EventHandler eh = Events [abortTransaction] as EventHandler;
  130. if (eh != null)
  131. eh (this, e);
  132. }
  133. [MonoTODO]
  134. public Control ParseControl (string content)
  135. {
  136. return null;
  137. }
  138. [MonoTODO]
  139. [EditorBrowsable (EditorBrowsableState.Never)]
  140. public static object ReadStringResource (Type t)
  141. {
  142. return null;
  143. }
  144. [MonoTODO]
  145. [EditorBrowsable (EditorBrowsableState.Never)]
  146. protected void SetStringResourcePointer (object stringResourcePointer,
  147. int maxResourceOffset)
  148. {
  149. }
  150. [MonoTODO]
  151. [EditorBrowsable (EditorBrowsableState.Never)]
  152. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  153. int size, bool fAsciiOnly)
  154. {
  155. }
  156. #endregion
  157. #region Events
  158. [WebSysDescription ("Raised when the user aborts a transaction.")]
  159. public event EventHandler AbortTransaction {
  160. add { Events.AddHandler (abortTransaction, value); }
  161. remove { Events.RemoveHandler (abortTransaction, value); }
  162. }
  163. [WebSysDescription ("Raised when the user initiates a transaction.")]
  164. public event EventHandler CommitTransaction {
  165. add { Events.AddHandler (commitTransaction, value); }
  166. remove { Events.RemoveHandler (commitTransaction, value); }
  167. }
  168. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  169. public event EventHandler Error {
  170. add { Events.AddHandler (error, value); }
  171. remove { Events.RemoveHandler (error, value); }
  172. }
  173. #endregion
  174. class SimpleTemplate : ITemplate
  175. {
  176. Type type;
  177. public SimpleTemplate (Type type)
  178. {
  179. this.type = type;
  180. }
  181. public void InstantiateIn (Control control)
  182. {
  183. Control template = Activator.CreateInstance (type) as Control;
  184. template.SetBindingContainer (false);
  185. control.Controls.Add (template);
  186. }
  187. }
  188. delegate void NoParamsDelegate ();
  189. class NoParamsInvoker
  190. {
  191. EventHandler faked;
  192. NoParamsDelegate real;
  193. public NoParamsInvoker (object o, string method)
  194. {
  195. real = (NoParamsDelegate) Delegate.CreateDelegate (
  196. typeof (NoParamsDelegate), o, method);
  197. faked = new EventHandler (InvokeNoParams);
  198. }
  199. void InvokeNoParams (object o, EventArgs args)
  200. {
  201. real ();
  202. }
  203. public EventHandler FakeDelegate {
  204. get { return faked; }
  205. }
  206. }
  207. }
  208. }