TemplateControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.Collections;
  32. using System.ComponentModel;
  33. using System.Reflection;
  34. using System.Security.Permissions;
  35. using System.Web.Compilation;
  36. using System.Web.Util;
  37. using System.Xml;
  38. namespace System.Web.UI {
  39. // CAS
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. #if NET_2_0
  43. public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
  44. #else
  45. public abstract class TemplateControl : Control, INamingContainer {
  46. #endif
  47. static object abortTransaction = new object ();
  48. static object commitTransaction = new object ();
  49. static object error = new object ();
  50. static string [] methodNames = { "Page_Init",
  51. #if NET_2_0
  52. "Page_PreInit",
  53. "Page_PreLoad",
  54. "Page_LoadComplete",
  55. "Page_PreRenderComplete",
  56. "Page_SaveStateComplete",
  57. "Page_InitComplete",
  58. #endif
  59. "Page_Load",
  60. "Page_DataBind",
  61. "Page_PreRender",
  62. "Page_Disposed",
  63. "Page_Error",
  64. "Page_Unload",
  65. "Page_AbortTransaction",
  66. "Page_CommitTransaction"};
  67. const BindingFlags bflags = BindingFlags.Public |
  68. BindingFlags.NonPublic |
  69. BindingFlags.Instance;
  70. #region Constructor
  71. protected TemplateControl ()
  72. {
  73. Construct ();
  74. }
  75. #endregion
  76. #region Properties
  77. [EditorBrowsable (EditorBrowsableState.Never)]
  78. #if NET_2_0
  79. [Obsolete]
  80. #endif
  81. protected virtual int AutoHandlers {
  82. get { return 0; }
  83. set { }
  84. }
  85. [EditorBrowsable (EditorBrowsableState.Never)]
  86. protected virtual bool SupportAutoEvents {
  87. get { return true; }
  88. }
  89. #if NET_2_0
  90. [MonoTODO ("NotImplementedException")]
  91. public string AppRelativeVirtualPath {
  92. get { throw new NotImplementedException(); }
  93. set { throw new NotImplementedException (); }
  94. }
  95. #endif
  96. #endregion
  97. #region Methods
  98. protected virtual void Construct ()
  99. {
  100. }
  101. [MonoTODO ("Not implemented")]
  102. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  103. int size,
  104. bool fAsciiOnly)
  105. {
  106. return null;
  107. }
  108. internal void WireupAutomaticEvents ()
  109. {
  110. if (!SupportAutoEvents || !AutoEventWireup)
  111. return;
  112. Type type = GetType ();
  113. foreach (string methodName in methodNames) {
  114. MethodInfo method = type.GetMethod (methodName, bflags);
  115. if (method == null)
  116. continue;
  117. if (method.DeclaringType != type) {
  118. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  119. !method.IsFamilyAndAssembly && !method.IsFamily)
  120. continue;
  121. }
  122. if (method.ReturnType != typeof (void))
  123. continue;
  124. ParameterInfo [] parms = method.GetParameters ();
  125. int length = parms.Length;
  126. bool noParams = (length == 0);
  127. if (!noParams && (length != 2 ||
  128. parms [0].ParameterType != typeof (object) ||
  129. parms [1].ParameterType != typeof (EventArgs)))
  130. continue;
  131. int pos = methodName.IndexOf ("_");
  132. string eventName = methodName.Substring (pos + 1);
  133. EventInfo evt = type.GetEvent (eventName);
  134. if (evt == null) {
  135. /* This should never happen */
  136. continue;
  137. }
  138. if (noParams) {
  139. NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
  140. evt.AddEventHandler (this, npi.FakeDelegate);
  141. } else {
  142. evt.AddEventHandler (this, Delegate.CreateDelegate (
  143. typeof (EventHandler), this, methodName));
  144. }
  145. }
  146. }
  147. [EditorBrowsable (EditorBrowsableState.Never)]
  148. protected virtual void FrameworkInitialize ()
  149. {
  150. }
  151. Type GetTypeFromControlPath (string virtualPath)
  152. {
  153. if (virtualPath == null)
  154. throw new ArgumentNullException ("virtualPath");
  155. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  156. string realpath = Context.Request.MapPath (vpath);
  157. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  158. }
  159. public Control LoadControl (string virtualPath)
  160. {
  161. #if NET_2_0
  162. if (virtualPath == null)
  163. throw new ArgumentNullException ("virtualPath");
  164. #else
  165. if (virtualPath == null)
  166. throw new HttpException ("virtualPath is null");
  167. #endif
  168. Type type = GetTypeFromControlPath (virtualPath);
  169. object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
  170. if (attrs != null && attrs.Length == 1) {
  171. PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
  172. PartialCachingControl ctrl = new PartialCachingControl (type);
  173. ctrl.VaryByParams = attr.VaryByParams;
  174. ctrl.VaryByControls = attr.VaryByControls;
  175. ctrl.VaryByCustom = attr.VaryByCustom;
  176. return ctrl;
  177. }
  178. object control = Activator.CreateInstance (type);
  179. if (control is UserControl)
  180. ((UserControl) control).InitializeAsUserControl (Page);
  181. return (Control) control;
  182. }
  183. public ITemplate LoadTemplate (string virtualPath)
  184. {
  185. #if NET_2_0
  186. if (virtualPath == null)
  187. throw new ArgumentNullException ("virtualPath");
  188. #else
  189. if (virtualPath == null)
  190. throw new HttpException ("virtualPath is null");
  191. #endif
  192. Type t = GetTypeFromControlPath (virtualPath);
  193. return new SimpleTemplate (t);
  194. }
  195. protected virtual void OnAbortTransaction (EventArgs e)
  196. {
  197. EventHandler eh = Events [abortTransaction] as EventHandler;
  198. if (eh != null)
  199. eh (this, e);
  200. }
  201. protected virtual void OnCommitTransaction (EventArgs e)
  202. {
  203. EventHandler eh = Events [commitTransaction] as EventHandler;
  204. if (eh != null)
  205. eh (this, e);
  206. }
  207. protected virtual void OnError (EventArgs e)
  208. {
  209. EventHandler eh = Events [error] as EventHandler;
  210. if (eh != null)
  211. eh (this, e);
  212. }
  213. [MonoTODO ("Not implemented, always returns null")]
  214. public Control ParseControl (string content)
  215. {
  216. if (content == null)
  217. throw new ArgumentNullException ("content");
  218. return null;
  219. }
  220. [EditorBrowsable (EditorBrowsableState.Never)]
  221. public
  222. #if !NET_2_0
  223. static
  224. #endif
  225. object ReadStringResource ()
  226. {
  227. throw new NotSupportedException ();
  228. }
  229. #if NET_2_0
  230. protected object GetGlobalResourceObject (string className, string resourceKey)
  231. {
  232. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  233. }
  234. [MonoTODO ("Not implemented")]
  235. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  236. {
  237. // FIXME: not sure how to implement that one yet
  238. throw new NotSupportedException();
  239. }
  240. protected object GetLocalResourceObject (string resourceKey)
  241. {
  242. return HttpContext.GetLocalResourceObject (Context.Request.CurrentExecutionFilePath, resourceKey);
  243. }
  244. protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  245. {
  246. // FIXME: not sure how to implement that one yet
  247. throw new NotSupportedException();
  248. }
  249. #endif
  250. [EditorBrowsable (EditorBrowsableState.Never)]
  251. public static object ReadStringResource (Type t)
  252. {
  253. throw new NotSupportedException ();
  254. }
  255. [MonoTODO ("Not implemented, does nothing")]
  256. [EditorBrowsable (EditorBrowsableState.Never)]
  257. protected void SetStringResourcePointer (object stringResourcePointer,
  258. int maxResourceOffset)
  259. {
  260. }
  261. [MonoTODO ("Not implemented, does nothing")]
  262. [EditorBrowsable (EditorBrowsableState.Never)]
  263. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  264. int size, bool fAsciiOnly)
  265. {
  266. }
  267. #endregion
  268. #region Events
  269. [WebSysDescription ("Raised when the user aborts a transaction.")]
  270. public event EventHandler AbortTransaction {
  271. add { Events.AddHandler (abortTransaction, value); }
  272. remove { Events.RemoveHandler (abortTransaction, value); }
  273. }
  274. [WebSysDescription ("Raised when the user initiates a transaction.")]
  275. public event EventHandler CommitTransaction {
  276. add { Events.AddHandler (commitTransaction, value); }
  277. remove { Events.RemoveHandler (commitTransaction, value); }
  278. }
  279. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  280. public event EventHandler Error {
  281. add { Events.AddHandler (error, value); }
  282. remove { Events.RemoveHandler (error, value); }
  283. }
  284. #endregion
  285. class SimpleTemplate : ITemplate
  286. {
  287. Type type;
  288. public SimpleTemplate (Type type)
  289. {
  290. this.type = type;
  291. }
  292. public void InstantiateIn (Control control)
  293. {
  294. Control template = Activator.CreateInstance (type) as Control;
  295. template.SetBindingContainer (false);
  296. control.Controls.Add (template);
  297. }
  298. }
  299. #if NET_2_0
  300. protected internal object Eval (string expression)
  301. {
  302. return DataBinder.Eval (Page.GetDataItem(), expression);
  303. }
  304. protected internal string Eval (string expression, string format)
  305. {
  306. return DataBinder.Eval (Page.GetDataItem(), expression, format);
  307. }
  308. protected internal object XPath (string xpathexpression)
  309. {
  310. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
  311. }
  312. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  313. {
  314. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  315. }
  316. protected internal string XPath (string xpathexpression, string format)
  317. {
  318. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
  319. }
  320. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  321. {
  322. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  323. }
  324. protected internal IEnumerable XPathSelect (string xpathexpression)
  325. {
  326. return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
  327. }
  328. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  329. {
  330. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  331. }
  332. // IFilterResolutionService
  333. [MonoTODO ("Not implemented")]
  334. int IFilterResolutionService.CompareFilters (string filter1, string filter2)
  335. {
  336. throw new NotImplementedException ();
  337. }
  338. [MonoTODO ("Not implemented")]
  339. bool IFilterResolutionService.EvaluateFilter (string filterName)
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. #endif
  344. }
  345. }