TemplateControl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.Web.UI.TemplateControl.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Reflection;
  13. using System.Web.Compilation;
  14. using System.Web.Util;
  15. namespace System.Web.UI {
  16. public abstract class TemplateControl : Control, INamingContainer
  17. {
  18. static object abortTransaction = new object ();
  19. static object commitTransaction = new object ();
  20. static object error = new object ();
  21. static string [] methodNames = { "Page_Init",
  22. "Page_Load",
  23. "Page_DataBind",
  24. "Page_PreRender",
  25. "Page_Dispose",
  26. "Page_Error" };
  27. const BindingFlags bflags = BindingFlags.Public |
  28. BindingFlags.NonPublic |
  29. BindingFlags.DeclaredOnly |
  30. BindingFlags.Static |
  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 virtual 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. protected virtual void FrameworkInitialize ()
  86. {
  87. }
  88. Type GetTypeFromControlPath (string virtualPath)
  89. {
  90. if (virtualPath == null)
  91. throw new ArgumentNullException ("virtualPath");
  92. return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
  93. }
  94. public Control LoadControl (string virtualPath)
  95. {
  96. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  97. if (control is UserControl)
  98. ((UserControl) control).InitializeAsUserControl (Page);
  99. return (Control) control;
  100. }
  101. public ITemplate LoadTemplate (string virtualPath)
  102. {
  103. Type t = GetTypeFromControlPath (virtualPath);
  104. return new SimpleTemplate (t);
  105. }
  106. protected virtual void OnAbortTransaction (EventArgs e)
  107. {
  108. EventHandler eh = Events [error] as EventHandler;
  109. if (eh != null)
  110. eh.Invoke (this, e);
  111. }
  112. protected virtual void OnCommitTransaction (EventArgs e)
  113. {
  114. EventHandler eh = Events [commitTransaction] as EventHandler;
  115. if (eh != null)
  116. eh.Invoke (this, e);
  117. }
  118. protected virtual void OnError (EventArgs e)
  119. {
  120. EventHandler eh = Events [abortTransaction] as EventHandler;
  121. if (eh != null)
  122. eh.Invoke (this, e);
  123. }
  124. [MonoTODO]
  125. public Control ParseControl (string content)
  126. {
  127. return null;
  128. }
  129. [MonoTODO]
  130. public static object ReadStringResource (Type t)
  131. {
  132. return null;
  133. }
  134. [MonoTODO]
  135. protected void SetStringResourcePointer (object stringResourcePointer,
  136. int maxResourceOffset)
  137. {
  138. }
  139. [MonoTODO]
  140. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  141. int size, bool fAsciiOnly)
  142. {
  143. }
  144. #endregion
  145. #region Events
  146. public event EventHandler AbortTransaction {
  147. add { Events.AddHandler (abortTransaction, value); }
  148. remove { Events.RemoveHandler (abortTransaction, value); }
  149. }
  150. public event EventHandler CommitTransaction {
  151. add { Events.AddHandler (commitTransaction, value); }
  152. remove { Events.RemoveHandler (commitTransaction, value); }
  153. }
  154. public event EventHandler Error {
  155. add { Events.AddHandler (error, value); }
  156. remove { Events.RemoveHandler (error, value); }
  157. }
  158. #endregion
  159. class SimpleTemplate : ITemplate
  160. {
  161. Type type;
  162. public SimpleTemplate (Type type)
  163. {
  164. this.type = type;
  165. }
  166. public void InstantiateIn (Control control)
  167. {
  168. Control template = Activator.CreateInstance (type) as Control;
  169. template.SetBindingContainer (false);
  170. control.Controls.Add (template);
  171. }
  172. }
  173. }
  174. }