2
0

TemplateControl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if (virtualPath [0] == '/')
  93. throw new ArgumentException ("Path cannot be rooted", "virtualPath");
  94. virtualPath = PathUtil.Combine (TemplateSourceDirectory, virtualPath);
  95. return UserControlCompiler.CompileUserControlType (new UserControlParser (virtualPath));
  96. }
  97. public Control LoadControl (string virtualPath)
  98. {
  99. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  100. if (control is UserControl)
  101. ((UserControl) control).InitializeAsUserControl (Page);
  102. return (Control) control;
  103. }
  104. public ITemplate LoadTemplate (string virtualPath)
  105. {
  106. Type t = GetTypeFromControlPath (virtualPath);
  107. return new SimpleTemplate (t);
  108. }
  109. protected virtual void OnAbortTransaction (EventArgs e)
  110. {
  111. EventHandler eh = Events [error] as EventHandler;
  112. if (eh != null)
  113. eh.Invoke (this, e);
  114. }
  115. protected virtual void OnCommitTransaction (EventArgs e)
  116. {
  117. EventHandler eh = Events [commitTransaction] as EventHandler;
  118. if (eh != null)
  119. eh.Invoke (this, e);
  120. }
  121. protected virtual void OnError (EventArgs e)
  122. {
  123. EventHandler eh = Events [abortTransaction] as EventHandler;
  124. if (eh != null)
  125. eh.Invoke (this, e);
  126. }
  127. [MonoTODO]
  128. public Control ParseControl (string content)
  129. {
  130. return null;
  131. }
  132. [MonoTODO]
  133. public static object ReadStringResource (Type t)
  134. {
  135. return null;
  136. }
  137. [MonoTODO]
  138. protected void SetStringResourcePointer (object stringResourcePointer,
  139. int maxResourceOffset)
  140. {
  141. }
  142. [MonoTODO]
  143. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  144. int size, bool fAsciiOnly)
  145. {
  146. }
  147. #endregion
  148. #region Events
  149. public event EventHandler AbortTransaction {
  150. add { Events.AddHandler (abortTransaction, value); }
  151. remove { Events.RemoveHandler (abortTransaction, value); }
  152. }
  153. public event EventHandler CommitTransaction {
  154. add { Events.AddHandler (commitTransaction, value); }
  155. remove { Events.RemoveHandler (commitTransaction, value); }
  156. }
  157. public event EventHandler Error {
  158. add { Events.AddHandler (error, value); }
  159. remove { Events.RemoveHandler (error, value); }
  160. }
  161. #endregion
  162. class SimpleTemplate : ITemplate
  163. {
  164. Type type;
  165. public SimpleTemplate (Type type)
  166. {
  167. this.type = type;
  168. }
  169. public void InstantiateIn (Control control)
  170. {
  171. Control template = Activator.CreateInstance (type) as Control;
  172. template.SetBindingContainer (false);
  173. control.Controls.Add (template);
  174. }
  175. }
  176. }
  177. }