TemplateControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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, method);
  148. evt.AddEventHandler (this, npi.FakeDelegate);
  149. } else {
  150. evt.AddEventHandler (this, Delegate.CreateDelegate (
  151. #if NET_2_0
  152. typeof (EventHandler), this, method));
  153. #else
  154. typeof (EventHandler), this, methodName));
  155. #endif
  156. }
  157. }
  158. }
  159. [EditorBrowsable (EditorBrowsableState.Never)]
  160. protected virtual void FrameworkInitialize ()
  161. {
  162. }
  163. Type GetTypeFromControlPath (string virtualPath)
  164. {
  165. if (virtualPath == null)
  166. throw new ArgumentNullException ("virtualPath");
  167. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  168. string realpath = Context.Request.MapPath (vpath);
  169. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  170. }
  171. public Control LoadControl (string virtualPath)
  172. {
  173. #if NET_2_0
  174. if (virtualPath == null)
  175. throw new ArgumentNullException ("virtualPath");
  176. #else
  177. if (virtualPath == null)
  178. throw new HttpException ("virtualPath is null");
  179. #endif
  180. Type type = GetTypeFromControlPath (virtualPath);
  181. return LoadControl (type, null);
  182. }
  183. public Control LoadControl (Type type, object[] parameters)
  184. {
  185. object [] attrs = type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
  186. if (attrs != null && attrs.Length == 1) {
  187. PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
  188. PartialCachingControl ctrl = new PartialCachingControl (type, parameters);
  189. ctrl.VaryByParams = attr.VaryByParams;
  190. ctrl.VaryByControls = attr.VaryByControls;
  191. ctrl.VaryByCustom = attr.VaryByCustom;
  192. return ctrl;
  193. }
  194. object control = Activator.CreateInstance (type, parameters);
  195. if (control is UserControl)
  196. ((UserControl) control).InitializeAsUserControl (Page);
  197. return (Control) control;
  198. }
  199. public ITemplate LoadTemplate (string virtualPath)
  200. {
  201. #if NET_2_0
  202. if (virtualPath == null)
  203. throw new ArgumentNullException ("virtualPath");
  204. #else
  205. if (virtualPath == null)
  206. throw new HttpException ("virtualPath is null");
  207. #endif
  208. Type t = GetTypeFromControlPath (virtualPath);
  209. return new SimpleTemplate (t);
  210. }
  211. protected virtual void OnAbortTransaction (EventArgs e)
  212. {
  213. EventHandler eh = Events [abortTransaction] as EventHandler;
  214. if (eh != null)
  215. eh (this, e);
  216. }
  217. protected virtual void OnCommitTransaction (EventArgs e)
  218. {
  219. EventHandler eh = Events [commitTransaction] as EventHandler;
  220. if (eh != null)
  221. eh (this, e);
  222. }
  223. protected virtual void OnError (EventArgs e)
  224. {
  225. EventHandler eh = Events [error] as EventHandler;
  226. if (eh != null)
  227. eh (this, e);
  228. }
  229. [MonoTODO ("Not implemented, always returns null")]
  230. public Control ParseControl (string content)
  231. {
  232. if (content == null)
  233. throw new ArgumentNullException ("content");
  234. return null;
  235. }
  236. [EditorBrowsable (EditorBrowsableState.Never)]
  237. public
  238. #if !NET_2_0
  239. static
  240. #endif
  241. object ReadStringResource ()
  242. {
  243. throw new NotSupportedException ();
  244. }
  245. #if NET_2_0
  246. protected object GetGlobalResourceObject (string className, string resourceKey)
  247. {
  248. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  249. }
  250. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  251. {
  252. if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) ||
  253. String.IsNullOrEmpty (className) || objType == null)
  254. return null;
  255. object globalObject = GetGlobalResourceObject (className, resourceKey);
  256. if (globalObject == null)
  257. return null;
  258. TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
  259. if (converter == null || !converter.CanConvertFrom (globalObject.GetType ()))
  260. return null;
  261. return converter.ConvertFrom (globalObject);
  262. }
  263. protected object GetLocalResourceObject (string resourceKey)
  264. {
  265. return HttpContext.GetLocalResourceObject (VirtualPathUtility.ToAbsolute (this.AppRelativeVirtualPath),
  266. resourceKey);
  267. }
  268. protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  269. {
  270. if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) || objType == null)
  271. return null;
  272. object localObject = GetLocalResourceObject (resourceKey);
  273. if (localObject == null)
  274. return null;
  275. TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
  276. if (converter == null || !converter.CanConvertFrom (localObject.GetType ()))
  277. return null;
  278. return converter.ConvertFrom (localObject);
  279. }
  280. #endif
  281. [EditorBrowsable (EditorBrowsableState.Never)]
  282. public static object ReadStringResource (Type t)
  283. {
  284. throw new NotSupportedException ();
  285. }
  286. [MonoTODO ("Not implemented, does nothing")]
  287. [EditorBrowsable (EditorBrowsableState.Never)]
  288. protected void SetStringResourcePointer (object stringResourcePointer,
  289. int maxResourceOffset)
  290. {
  291. }
  292. [MonoTODO ("Not implemented, does nothing")]
  293. [EditorBrowsable (EditorBrowsableState.Never)]
  294. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  295. int size, bool fAsciiOnly)
  296. {
  297. }
  298. #endregion
  299. #region Events
  300. [WebSysDescription ("Raised when the user aborts a transaction.")]
  301. public event EventHandler AbortTransaction {
  302. add { Events.AddHandler (abortTransaction, value); }
  303. remove { Events.RemoveHandler (abortTransaction, value); }
  304. }
  305. [WebSysDescription ("Raised when the user initiates a transaction.")]
  306. public event EventHandler CommitTransaction {
  307. add { Events.AddHandler (commitTransaction, value); }
  308. remove { Events.RemoveHandler (commitTransaction, value); }
  309. }
  310. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  311. public event EventHandler Error {
  312. add { Events.AddHandler (error, value); }
  313. remove { Events.RemoveHandler (error, value); }
  314. }
  315. #endregion
  316. class SimpleTemplate : ITemplate
  317. {
  318. Type type;
  319. public SimpleTemplate (Type type)
  320. {
  321. this.type = type;
  322. }
  323. public void InstantiateIn (Control control)
  324. {
  325. Control template = Activator.CreateInstance (type) as Control;
  326. template.SetBindingContainer (false);
  327. control.Controls.Add (template);
  328. }
  329. }
  330. #if NET_2_0
  331. protected internal object Eval (string expression)
  332. {
  333. return DataBinder.Eval (Page.GetDataItem(), expression);
  334. }
  335. protected internal string Eval (string expression, string format)
  336. {
  337. return DataBinder.Eval (Page.GetDataItem(), expression, format);
  338. }
  339. protected internal object XPath (string xpathexpression)
  340. {
  341. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
  342. }
  343. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  344. {
  345. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  346. }
  347. protected internal string XPath (string xpathexpression, string format)
  348. {
  349. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
  350. }
  351. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  352. {
  353. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  354. }
  355. protected internal IEnumerable XPathSelect (string xpathexpression)
  356. {
  357. return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
  358. }
  359. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  360. {
  361. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  362. }
  363. // IFilterResolutionService
  364. [MonoTODO ("Not implemented")]
  365. int IFilterResolutionService.CompareFilters (string filter1, string filter2)
  366. {
  367. throw new NotImplementedException ();
  368. }
  369. [MonoTODO ("Not implemented")]
  370. bool IFilterResolutionService.EvaluateFilter (string filterName)
  371. {
  372. throw new NotImplementedException ();
  373. }
  374. #endif
  375. }
  376. }