2
0

TemplateControl.cs 5.1 KB

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