TemplateControl.cs 11 KB

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