TemplateControl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.Instance;
  30. #region Constructor
  31. protected TemplateControl ()
  32. {
  33. Construct ();
  34. }
  35. #endregion
  36. #region Properties
  37. protected virtual int AutoHandlers {
  38. get { return 0; }
  39. set { }
  40. }
  41. protected virtual bool SupportAutoEvents {
  42. get { return true; }
  43. }
  44. #endregion
  45. #region Methods
  46. protected virtual void Construct ()
  47. {
  48. }
  49. [MonoTODO]
  50. protected virtual LiteralControl CreateResourceBasedLiteralControl (int offset,
  51. int size,
  52. bool fAsciiOnly)
  53. {
  54. return null;
  55. }
  56. internal void WireupAutomaticEvents ()
  57. {
  58. if (!SupportAutoEvents || !AutoEventWireup)
  59. return;
  60. Type type = GetType ();
  61. foreach (MethodInfo method in type.GetMethods (bflags)) {
  62. int pos = Array.IndexOf (methodNames, method.Name);
  63. if (pos == -1)
  64. continue;
  65. string name = methodNames [pos];
  66. pos = name.IndexOf ("_");
  67. if (pos == -1 || pos + 1 == name.Length)
  68. continue;
  69. if (method.ReturnType != typeof (void))
  70. continue;
  71. ParameterInfo [] parms = method.GetParameters ();
  72. if (parms.Length != 2 ||
  73. parms [0].ParameterType != typeof (object) ||
  74. parms [1].ParameterType != typeof (EventArgs))
  75. continue;
  76. string eventName = name.Substring (pos + 1);
  77. EventInfo evt = type.GetEvent (eventName);
  78. if (evt == null)
  79. continue;
  80. evt.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), method));
  81. }
  82. }
  83. protected virtual void FrameworkInitialize ()
  84. {
  85. }
  86. Type GetTypeFromControlPath (string virtualPath)
  87. {
  88. if (virtualPath == null)
  89. throw new ArgumentNullException ("virtualPath");
  90. return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
  91. }
  92. public Control LoadControl (string virtualPath)
  93. {
  94. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  95. if (control is UserControl)
  96. ((UserControl) control).InitializeAsUserControl (Page);
  97. return (Control) control;
  98. }
  99. public ITemplate LoadTemplate (string virtualPath)
  100. {
  101. Type t = GetTypeFromControlPath (virtualPath);
  102. return new SimpleTemplate (t);
  103. }
  104. protected virtual void OnAbortTransaction (EventArgs e)
  105. {
  106. EventHandler eh = Events [error] as EventHandler;
  107. if (eh != null)
  108. eh.Invoke (this, e);
  109. }
  110. protected virtual void OnCommitTransaction (EventArgs e)
  111. {
  112. EventHandler eh = Events [commitTransaction] as EventHandler;
  113. if (eh != null)
  114. eh.Invoke (this, e);
  115. }
  116. protected virtual void OnError (EventArgs e)
  117. {
  118. EventHandler eh = Events [abortTransaction] as EventHandler;
  119. if (eh != null)
  120. eh.Invoke (this, e);
  121. }
  122. [MonoTODO]
  123. public Control ParseControl (string content)
  124. {
  125. return null;
  126. }
  127. [MonoTODO]
  128. public static object ReadStringResource (Type t)
  129. {
  130. return null;
  131. }
  132. [MonoTODO]
  133. protected void SetStringResourcePointer (object stringResourcePointer,
  134. int maxResourceOffset)
  135. {
  136. }
  137. [MonoTODO]
  138. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  139. int size, bool fAsciiOnly)
  140. {
  141. }
  142. #endregion
  143. #region Events
  144. public event EventHandler AbortTransaction {
  145. add { Events.AddHandler (abortTransaction, value); }
  146. remove { Events.RemoveHandler (abortTransaction, value); }
  147. }
  148. public event EventHandler CommitTransaction {
  149. add { Events.AddHandler (commitTransaction, value); }
  150. remove { Events.RemoveHandler (commitTransaction, value); }
  151. }
  152. public event EventHandler Error {
  153. add { Events.AddHandler (error, value); }
  154. remove { Events.RemoveHandler (error, value); }
  155. }
  156. #endregion
  157. class SimpleTemplate : ITemplate
  158. {
  159. Type type;
  160. public SimpleTemplate (Type type)
  161. {
  162. this.type = type;
  163. }
  164. public void InstantiateIn (Control control)
  165. {
  166. Control template = Activator.CreateInstance (type) as Control;
  167. template.SetBindingContainer (false);
  168. control.Controls.Add (template);
  169. }
  170. }
  171. }
  172. }