2
0

TemplateControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 (MethodInfo method in type.GetMethods (bflags)) {
  65. int pos = Array.IndexOf (methodNames, method.Name);
  66. if (pos == -1)
  67. continue;
  68. string name = methodNames [pos];
  69. pos = name.IndexOf ("_");
  70. if (pos == -1 || pos + 1 == name.Length)
  71. continue;
  72. if (method.ReturnType != typeof (void))
  73. continue;
  74. ParameterInfo [] parms = method.GetParameters ();
  75. int length = parms.Length;
  76. bool noParams = (length == 0);
  77. if (!noParams && (parms.Length != 2 ||
  78. parms [0].ParameterType != typeof (object) ||
  79. parms [1].ParameterType != typeof (EventArgs)))
  80. continue;
  81. string eventName = name.Substring (pos + 1);
  82. EventInfo evt = type.GetEvent (eventName);
  83. if (evt == null)
  84. continue;
  85. if (noParams) {
  86. NoParamsInvoker npi = new NoParamsInvoker (this, method.Name);
  87. evt.AddEventHandler (this, npi.FakeDelegate);
  88. } else {
  89. evt.AddEventHandler (this, Delegate.CreateDelegate (
  90. typeof (EventHandler), this, method.Name));
  91. }
  92. }
  93. }
  94. [EditorBrowsable (EditorBrowsableState.Never)]
  95. protected virtual void FrameworkInitialize ()
  96. {
  97. }
  98. Type GetTypeFromControlPath (string virtualPath)
  99. {
  100. if (virtualPath == null)
  101. throw new ArgumentNullException ("virtualPath");
  102. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  103. string realpath = Context.Request.MapPath (vpath);
  104. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  105. }
  106. public Control LoadControl (string virtualPath)
  107. {
  108. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  109. if (control is UserControl)
  110. ((UserControl) control).InitializeAsUserControl (Page);
  111. return (Control) control;
  112. }
  113. public ITemplate LoadTemplate (string virtualPath)
  114. {
  115. Type t = GetTypeFromControlPath (virtualPath);
  116. return new SimpleTemplate (t);
  117. }
  118. protected virtual void OnAbortTransaction (EventArgs e)
  119. {
  120. EventHandler eh = Events [error] as EventHandler;
  121. if (eh != null)
  122. eh (this, e);
  123. }
  124. protected virtual void OnCommitTransaction (EventArgs e)
  125. {
  126. EventHandler eh = Events [commitTransaction] as EventHandler;
  127. if (eh != null)
  128. eh (this, e);
  129. }
  130. protected virtual void OnError (EventArgs e)
  131. {
  132. EventHandler eh = Events [abortTransaction] as EventHandler;
  133. if (eh != null)
  134. eh (this, e);
  135. }
  136. [MonoTODO]
  137. public Control ParseControl (string content)
  138. {
  139. return null;
  140. }
  141. [MonoTODO]
  142. [EditorBrowsable (EditorBrowsableState.Never)]
  143. public static object ReadStringResource (Type t)
  144. {
  145. return null;
  146. }
  147. [MonoTODO]
  148. [EditorBrowsable (EditorBrowsableState.Never)]
  149. protected void SetStringResourcePointer (object stringResourcePointer,
  150. int maxResourceOffset)
  151. {
  152. }
  153. [MonoTODO]
  154. [EditorBrowsable (EditorBrowsableState.Never)]
  155. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  156. int size, bool fAsciiOnly)
  157. {
  158. }
  159. #endregion
  160. #region Events
  161. [WebSysDescription ("Raised when the user aborts a transaction.")]
  162. public event EventHandler AbortTransaction {
  163. add { Events.AddHandler (abortTransaction, value); }
  164. remove { Events.RemoveHandler (abortTransaction, value); }
  165. }
  166. [WebSysDescription ("Raised when the user initiates a transaction.")]
  167. public event EventHandler CommitTransaction {
  168. add { Events.AddHandler (commitTransaction, value); }
  169. remove { Events.RemoveHandler (commitTransaction, value); }
  170. }
  171. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  172. public event EventHandler Error {
  173. add { Events.AddHandler (error, value); }
  174. remove { Events.RemoveHandler (error, value); }
  175. }
  176. #endregion
  177. class SimpleTemplate : ITemplate
  178. {
  179. Type type;
  180. public SimpleTemplate (Type type)
  181. {
  182. this.type = type;
  183. }
  184. public void InstantiateIn (Control control)
  185. {
  186. Control template = Activator.CreateInstance (type) as Control;
  187. template.SetBindingContainer (false);
  188. control.Controls.Add (template);
  189. }
  190. }
  191. }
  192. }