TemplateControl.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Reflection;
  35. using System.Web;
  36. using System.Web.Compilation;
  37. using System.Web.Util;
  38. namespace System.Web.UI {
  39. public abstract class TemplateControl : Control, INamingContainer
  40. {
  41. static object abortTransaction = new object ();
  42. static object commitTransaction = new object ();
  43. static object error = new object ();
  44. static string [] methodNames = { "Page_Init",
  45. "Page_Load",
  46. "Page_DataBind",
  47. "Page_PreRender",
  48. "Page_Disposed",
  49. "Page_Error",
  50. "Page_Unload",
  51. "Page_AbortTransaction",
  52. "Page_CommitTransaction" };
  53. const BindingFlags bflags = BindingFlags.Public |
  54. BindingFlags.NonPublic |
  55. BindingFlags.Instance;
  56. #region Constructor
  57. protected TemplateControl ()
  58. {
  59. Construct ();
  60. }
  61. #endregion
  62. #region Properties
  63. [EditorBrowsable (EditorBrowsableState.Never)]
  64. protected virtual int AutoHandlers {
  65. get { return 0; }
  66. set { }
  67. }
  68. [EditorBrowsable (EditorBrowsableState.Never)]
  69. protected virtual bool SupportAutoEvents {
  70. get { return true; }
  71. }
  72. #endregion
  73. #region Methods
  74. protected virtual void Construct ()
  75. {
  76. }
  77. [MonoTODO]
  78. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  79. int size,
  80. bool fAsciiOnly)
  81. {
  82. return null;
  83. }
  84. internal void WireupAutomaticEvents ()
  85. {
  86. if (!SupportAutoEvents || !AutoEventWireup)
  87. return;
  88. Type type = GetType ();
  89. foreach (string methodName in methodNames) {
  90. MethodInfo method = type.GetMethod (methodName, bflags);
  91. if (method == null)
  92. continue;
  93. if (method.ReturnType != typeof (void))
  94. continue;
  95. ParameterInfo [] parms = method.GetParameters ();
  96. int length = parms.Length;
  97. bool noParams = (length == 0);
  98. if (!noParams && (length != 2 ||
  99. parms [0].ParameterType != typeof (object) ||
  100. parms [1].ParameterType != typeof (EventArgs)))
  101. continue;
  102. int pos = methodName.IndexOf ("_");
  103. string eventName = methodName.Substring (pos + 1);
  104. EventInfo evt = type.GetEvent (eventName);
  105. if (evt == null) {
  106. /* This should never happen */
  107. continue;
  108. }
  109. if (noParams) {
  110. NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
  111. evt.AddEventHandler (this, npi.FakeDelegate);
  112. } else {
  113. evt.AddEventHandler (this, Delegate.CreateDelegate (
  114. typeof (EventHandler), this, methodName));
  115. }
  116. }
  117. }
  118. [EditorBrowsable (EditorBrowsableState.Never)]
  119. protected virtual void FrameworkInitialize ()
  120. {
  121. }
  122. Type GetTypeFromControlPath (string virtualPath)
  123. {
  124. if (virtualPath == null)
  125. throw new ArgumentNullException ("virtualPath");
  126. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  127. string realpath = Context.Request.MapPath (vpath);
  128. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  129. }
  130. public Control LoadControl (string virtualPath)
  131. {
  132. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  133. if (control is UserControl)
  134. ((UserControl) control).InitializeAsUserControl (Page);
  135. return (Control) control;
  136. }
  137. public ITemplate LoadTemplate (string virtualPath)
  138. {
  139. Type t = GetTypeFromControlPath (virtualPath);
  140. return new SimpleTemplate (t);
  141. }
  142. protected virtual void OnAbortTransaction (EventArgs e)
  143. {
  144. EventHandler eh = Events [abortTransaction] as EventHandler;
  145. if (eh != null)
  146. eh (this, e);
  147. }
  148. protected virtual void OnCommitTransaction (EventArgs e)
  149. {
  150. EventHandler eh = Events [commitTransaction] as EventHandler;
  151. if (eh != null)
  152. eh (this, e);
  153. }
  154. protected virtual void OnError (EventArgs e)
  155. {
  156. EventHandler eh = Events [error] as EventHandler;
  157. if (eh != null)
  158. eh (this, e);
  159. }
  160. [MonoTODO]
  161. public Control ParseControl (string content)
  162. {
  163. return null;
  164. }
  165. [MonoTODO]
  166. [EditorBrowsable (EditorBrowsableState.Never)]
  167. public static object ReadStringResource (Type t)
  168. {
  169. return null;
  170. }
  171. [MonoTODO]
  172. [EditorBrowsable (EditorBrowsableState.Never)]
  173. protected void SetStringResourcePointer (object stringResourcePointer,
  174. int maxResourceOffset)
  175. {
  176. }
  177. [MonoTODO]
  178. [EditorBrowsable (EditorBrowsableState.Never)]
  179. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  180. int size, bool fAsciiOnly)
  181. {
  182. }
  183. #endregion
  184. #region Events
  185. [WebSysDescription ("Raised when the user aborts a transaction.")]
  186. public event EventHandler AbortTransaction {
  187. add { Events.AddHandler (abortTransaction, value); }
  188. remove { Events.RemoveHandler (abortTransaction, value); }
  189. }
  190. [WebSysDescription ("Raised when the user initiates a transaction.")]
  191. public event EventHandler CommitTransaction {
  192. add { Events.AddHandler (commitTransaction, value); }
  193. remove { Events.RemoveHandler (commitTransaction, value); }
  194. }
  195. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  196. public event EventHandler Error {
  197. add { Events.AddHandler (error, value); }
  198. remove { Events.RemoveHandler (error, value); }
  199. }
  200. #endregion
  201. class SimpleTemplate : ITemplate
  202. {
  203. Type type;
  204. public SimpleTemplate (Type type)
  205. {
  206. this.type = type;
  207. }
  208. public void InstantiateIn (Control control)
  209. {
  210. Control template = Activator.CreateInstance (type) as Control;
  211. template.SetBindingContainer (false);
  212. control.Controls.Add (template);
  213. }
  214. }
  215. }
  216. }