TemplateControl.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.DeclaringType != type) {
  94. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  95. !method.IsFamilyAndAssembly && !method.IsFamily)
  96. continue;
  97. }
  98. if (method.ReturnType != typeof (void))
  99. continue;
  100. ParameterInfo [] parms = method.GetParameters ();
  101. int length = parms.Length;
  102. bool noParams = (length == 0);
  103. if (!noParams && (length != 2 ||
  104. parms [0].ParameterType != typeof (object) ||
  105. parms [1].ParameterType != typeof (EventArgs)))
  106. continue;
  107. int pos = methodName.IndexOf ("_");
  108. string eventName = methodName.Substring (pos + 1);
  109. EventInfo evt = type.GetEvent (eventName);
  110. if (evt == null) {
  111. /* This should never happen */
  112. continue;
  113. }
  114. if (noParams) {
  115. NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
  116. evt.AddEventHandler (this, npi.FakeDelegate);
  117. } else {
  118. evt.AddEventHandler (this, Delegate.CreateDelegate (
  119. typeof (EventHandler), this, methodName));
  120. }
  121. }
  122. }
  123. [EditorBrowsable (EditorBrowsableState.Never)]
  124. protected virtual void FrameworkInitialize ()
  125. {
  126. }
  127. Type GetTypeFromControlPath (string virtualPath)
  128. {
  129. if (virtualPath == null)
  130. throw new ArgumentNullException ("virtualPath");
  131. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  132. string realpath = Context.Request.MapPath (vpath);
  133. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  134. }
  135. public Control LoadControl (string virtualPath)
  136. {
  137. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  138. if (control is UserControl)
  139. ((UserControl) control).InitializeAsUserControl (Page);
  140. return (Control) control;
  141. }
  142. public ITemplate LoadTemplate (string virtualPath)
  143. {
  144. Type t = GetTypeFromControlPath (virtualPath);
  145. return new SimpleTemplate (t);
  146. }
  147. protected virtual void OnAbortTransaction (EventArgs e)
  148. {
  149. EventHandler eh = Events [abortTransaction] as EventHandler;
  150. if (eh != null)
  151. eh (this, e);
  152. }
  153. protected virtual void OnCommitTransaction (EventArgs e)
  154. {
  155. EventHandler eh = Events [commitTransaction] as EventHandler;
  156. if (eh != null)
  157. eh (this, e);
  158. }
  159. protected virtual void OnError (EventArgs e)
  160. {
  161. EventHandler eh = Events [error] as EventHandler;
  162. if (eh != null)
  163. eh (this, e);
  164. }
  165. [MonoTODO]
  166. public Control ParseControl (string content)
  167. {
  168. return null;
  169. }
  170. [MonoTODO]
  171. [EditorBrowsable (EditorBrowsableState.Never)]
  172. public static object ReadStringResource (Type t)
  173. {
  174. return null;
  175. }
  176. [MonoTODO]
  177. [EditorBrowsable (EditorBrowsableState.Never)]
  178. protected void SetStringResourcePointer (object stringResourcePointer,
  179. int maxResourceOffset)
  180. {
  181. }
  182. [MonoTODO]
  183. [EditorBrowsable (EditorBrowsableState.Never)]
  184. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  185. int size, bool fAsciiOnly)
  186. {
  187. }
  188. #endregion
  189. #region Events
  190. [WebSysDescription ("Raised when the user aborts a transaction.")]
  191. public event EventHandler AbortTransaction {
  192. add { Events.AddHandler (abortTransaction, value); }
  193. remove { Events.RemoveHandler (abortTransaction, value); }
  194. }
  195. [WebSysDescription ("Raised when the user initiates a transaction.")]
  196. public event EventHandler CommitTransaction {
  197. add { Events.AddHandler (commitTransaction, value); }
  198. remove { Events.RemoveHandler (commitTransaction, value); }
  199. }
  200. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  201. public event EventHandler Error {
  202. add { Events.AddHandler (error, value); }
  203. remove { Events.RemoveHandler (error, value); }
  204. }
  205. #endregion
  206. class SimpleTemplate : ITemplate
  207. {
  208. Type type;
  209. public SimpleTemplate (Type type)
  210. {
  211. this.type = type;
  212. }
  213. public void InstantiateIn (Control control)
  214. {
  215. Control template = Activator.CreateInstance (type) as Control;
  216. template.SetBindingContainer (false);
  217. control.Controls.Add (template);
  218. }
  219. }
  220. #if NET_2_0
  221. Stack dataItemCtx;
  222. internal void PushDataItemContext (object o)
  223. {
  224. if (dataItemCtx == null)
  225. dataItemCtx = new Stack ();
  226. dataItemCtx.Push (o);
  227. }
  228. internal void PopDataItemContext ()
  229. {
  230. if (dataItemCtx == null)
  231. throw new InvalidOperationException ();
  232. dataItemCtx.Pop ();
  233. }
  234. internal object CurrentDataItem {
  235. get {
  236. if (dataItemCtx == null)
  237. throw new InvalidOperationException ("No data item");
  238. return dataItemCtx.Peek ();
  239. }
  240. }
  241. protected object Eval (string expression)
  242. {
  243. return DataBinder.Eval (CurrentDataItem, expression);
  244. }
  245. protected object Eval (string expression, string format)
  246. {
  247. return DataBinder.Eval (CurrentDataItem, expression, format);
  248. }
  249. protected object XPath (string xpathexpression)
  250. {
  251. return XPathBinder.Eval (CurrentDataItem, xpathexpression);
  252. }
  253. protected object XPath (string xpathexpression, string format)
  254. {
  255. return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
  256. }
  257. protected IEnumerable XPathSelect (string xpathexpression)
  258. {
  259. return XPathBinder.Select (CurrentDataItem, xpathexpression);
  260. }
  261. #endif
  262. }
  263. }