TemplateControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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;
  16. using System.Web.Compilation;
  17. using System.Web.Util;
  18. namespace System.Web.UI {
  19. public abstract class TemplateControl : Control, INamingContainer
  20. {
  21. static object abortTransaction = new object ();
  22. static object commitTransaction = new object ();
  23. static object error = new object ();
  24. static string [] methodNames = { "Page_Init",
  25. "Page_Load",
  26. "Page_DataBind",
  27. "Page_PreRender",
  28. "Page_Dispose",
  29. "Page_Error" };
  30. const BindingFlags bflags = BindingFlags.Public |
  31. BindingFlags.NonPublic |
  32. BindingFlags.Instance;
  33. #region Constructor
  34. protected TemplateControl ()
  35. {
  36. Construct ();
  37. }
  38. #endregion
  39. #region Properties
  40. protected virtual int AutoHandlers {
  41. get { return 0; }
  42. set { }
  43. }
  44. protected virtual bool SupportAutoEvents {
  45. get { return true; }
  46. }
  47. #endregion
  48. #region Methods
  49. protected virtual void Construct ()
  50. {
  51. }
  52. [MonoTODO]
  53. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  54. int size,
  55. bool fAsciiOnly)
  56. {
  57. return null;
  58. }
  59. internal void WireupAutomaticEvents ()
  60. {
  61. if (!SupportAutoEvents || !AutoEventWireup)
  62. return;
  63. Type type = GetType ();
  64. foreach (string methodName in methodNames) {
  65. MethodInfo method = type.GetMethod (methodName, bflags);
  66. if (method == null)
  67. continue;
  68. if (method.ReturnType != typeof (void))
  69. continue;
  70. ParameterInfo [] parms = method.GetParameters ();
  71. int length = parms.Length;
  72. bool noParams = (length == 0);
  73. if (!noParams && (length != 2 ||
  74. parms [0].ParameterType != typeof (object) ||
  75. parms [1].ParameterType != typeof (EventArgs)))
  76. continue;
  77. int pos = methodName.IndexOf ("_");
  78. string eventName = methodName.Substring (pos + 1);
  79. EventInfo evt = type.GetEvent (eventName);
  80. if (evt == null) {
  81. /* This should never happen */
  82. continue;
  83. }
  84. if (noParams) {
  85. NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
  86. evt.AddEventHandler (this, npi.FakeDelegate);
  87. } else {
  88. evt.AddEventHandler (this, Delegate.CreateDelegate (
  89. typeof (EventHandler), this, methodName));
  90. }
  91. }
  92. }
  93. [EditorBrowsable (EditorBrowsableState.Never)]
  94. protected virtual void FrameworkInitialize ()
  95. {
  96. }
  97. Type GetTypeFromControlPath (string virtualPath)
  98. {
  99. if (virtualPath == null)
  100. throw new ArgumentNullException ("virtualPath");
  101. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  102. string realpath = Context.Request.MapPath (vpath);
  103. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  104. }
  105. public Control LoadControl (string virtualPath)
  106. {
  107. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  108. if (control is UserControl)
  109. ((UserControl) control).InitializeAsUserControl (Page);
  110. return (Control) control;
  111. }
  112. public ITemplate LoadTemplate (string virtualPath)
  113. {
  114. Type t = GetTypeFromControlPath (virtualPath);
  115. return new SimpleTemplate (t);
  116. }
  117. protected virtual void OnAbortTransaction (EventArgs e)
  118. {
  119. EventHandler eh = Events [error] as EventHandler;
  120. if (eh != null)
  121. eh (this, e);
  122. }
  123. protected virtual void OnCommitTransaction (EventArgs e)
  124. {
  125. EventHandler eh = Events [commitTransaction] as EventHandler;
  126. if (eh != null)
  127. eh (this, e);
  128. }
  129. protected virtual void OnError (EventArgs e)
  130. {
  131. EventHandler eh = Events [abortTransaction] as EventHandler;
  132. if (eh != null)
  133. eh (this, e);
  134. }
  135. [MonoTODO]
  136. public Control ParseControl (string content)
  137. {
  138. return null;
  139. }
  140. [MonoTODO]
  141. [EditorBrowsable (EditorBrowsableState.Never)]
  142. public static object ReadStringResource (Type t)
  143. {
  144. return null;
  145. }
  146. [MonoTODO]
  147. [EditorBrowsable (EditorBrowsableState.Never)]
  148. protected void SetStringResourcePointer (object stringResourcePointer,
  149. int maxResourceOffset)
  150. {
  151. }
  152. [MonoTODO]
  153. [EditorBrowsable (EditorBrowsableState.Never)]
  154. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  155. int size, bool fAsciiOnly)
  156. {
  157. }
  158. #endregion
  159. #region Events
  160. [WebSysDescription ("Raised when the user aborts a transaction.")]
  161. public event EventHandler AbortTransaction {
  162. add { Events.AddHandler (abortTransaction, value); }
  163. remove { Events.RemoveHandler (abortTransaction, value); }
  164. }
  165. [WebSysDescription ("Raised when the user initiates a transaction.")]
  166. public event EventHandler CommitTransaction {
  167. add { Events.AddHandler (commitTransaction, value); }
  168. remove { Events.RemoveHandler (commitTransaction, value); }
  169. }
  170. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  171. public event EventHandler Error {
  172. add { Events.AddHandler (error, value); }
  173. remove { Events.RemoveHandler (error, value); }
  174. }
  175. #endregion
  176. class SimpleTemplate : ITemplate
  177. {
  178. Type type;
  179. public SimpleTemplate (Type type)
  180. {
  181. this.type = type;
  182. }
  183. public void InstantiateIn (Control control)
  184. {
  185. Control template = Activator.CreateInstance (type) as Control;
  186. template.SetBindingContainer (false);
  187. control.Controls.Add (template);
  188. }
  189. }
  190. }
  191. }