TemplateControl.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. if (parms.Length != 2 ||
  75. parms [0].ParameterType != typeof (object) ||
  76. parms [1].ParameterType != typeof (EventArgs))
  77. continue;
  78. string eventName = name.Substring (pos + 1);
  79. EventInfo evt = type.GetEvent (eventName);
  80. if (evt == null)
  81. continue;
  82. evt.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), method));
  83. }
  84. }
  85. [EditorBrowsable (EditorBrowsableState.Never)]
  86. protected virtual void FrameworkInitialize ()
  87. {
  88. }
  89. Type GetTypeFromControlPath (string virtualPath)
  90. {
  91. if (virtualPath == null)
  92. throw new ArgumentNullException ("virtualPath");
  93. return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
  94. }
  95. public Control LoadControl (string virtualPath)
  96. {
  97. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  98. if (control is UserControl)
  99. ((UserControl) control).InitializeAsUserControl (Page);
  100. return (Control) control;
  101. }
  102. public ITemplate LoadTemplate (string virtualPath)
  103. {
  104. Type t = GetTypeFromControlPath (virtualPath);
  105. return new SimpleTemplate (t);
  106. }
  107. protected virtual void OnAbortTransaction (EventArgs e)
  108. {
  109. EventHandler eh = Events [error] as EventHandler;
  110. if (eh != null)
  111. eh.Invoke (this, e);
  112. }
  113. protected virtual void OnCommitTransaction (EventArgs e)
  114. {
  115. EventHandler eh = Events [commitTransaction] as EventHandler;
  116. if (eh != null)
  117. eh.Invoke (this, e);
  118. }
  119. protected virtual void OnError (EventArgs e)
  120. {
  121. EventHandler eh = Events [abortTransaction] as EventHandler;
  122. if (eh != null)
  123. eh.Invoke (this, e);
  124. }
  125. [MonoTODO]
  126. public Control ParseControl (string content)
  127. {
  128. return null;
  129. }
  130. [MonoTODO]
  131. [EditorBrowsable (EditorBrowsableState.Never)]
  132. public static object ReadStringResource (Type t)
  133. {
  134. return null;
  135. }
  136. [MonoTODO]
  137. [EditorBrowsable (EditorBrowsableState.Never)]
  138. protected void SetStringResourcePointer (object stringResourcePointer,
  139. int maxResourceOffset)
  140. {
  141. }
  142. [MonoTODO]
  143. [EditorBrowsable (EditorBrowsableState.Never)]
  144. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  145. int size, bool fAsciiOnly)
  146. {
  147. }
  148. #endregion
  149. #region Events
  150. [WebSysDescription ("Raised when the user aborts a transaction.")]
  151. public event EventHandler AbortTransaction {
  152. add { Events.AddHandler (abortTransaction, value); }
  153. remove { Events.RemoveHandler (abortTransaction, value); }
  154. }
  155. [WebSysDescription ("Raised when the user initiates a transaction.")]
  156. public event EventHandler CommitTransaction {
  157. add { Events.AddHandler (commitTransaction, value); }
  158. remove { Events.RemoveHandler (commitTransaction, value); }
  159. }
  160. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  161. public event EventHandler Error {
  162. add { Events.AddHandler (error, value); }
  163. remove { Events.RemoveHandler (error, value); }
  164. }
  165. #endregion
  166. class SimpleTemplate : ITemplate
  167. {
  168. Type type;
  169. public SimpleTemplate (Type type)
  170. {
  171. this.type = type;
  172. }
  173. public void InstantiateIn (Control control)
  174. {
  175. Control template = Activator.CreateInstance (type) as Control;
  176. template.SetBindingContainer (false);
  177. control.Controls.Add (template);
  178. }
  179. }
  180. }
  181. }