TemplateControl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. using System.IO;
  39. namespace System.Web.UI {
  40. // CAS
  41. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. #if NET_2_0
  44. public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
  45. #else
  46. public abstract class TemplateControl : Control, INamingContainer {
  47. #endif
  48. static readonly Assembly _System_Web_Assembly = typeof (TemplateControl).Assembly;
  49. static object abortTransaction = new object ();
  50. static object commitTransaction = new object ();
  51. static object error = new object ();
  52. static string [] methodNames = { "Page_Init",
  53. #if NET_2_0
  54. "Page_PreInit",
  55. "Page_PreLoad",
  56. "Page_LoadComplete",
  57. "Page_PreRenderComplete",
  58. "Page_SaveStateComplete",
  59. "Page_InitComplete",
  60. #endif
  61. "Page_Load",
  62. "Page_DataBind",
  63. "Page_PreRender",
  64. "Page_Disposed",
  65. "Page_Error",
  66. "Page_Unload",
  67. "Page_AbortTransaction",
  68. "Page_CommitTransaction"};
  69. const BindingFlags bflags = BindingFlags.Public |
  70. BindingFlags.NonPublic |
  71. BindingFlags.Instance;
  72. #if NET_2_0
  73. string _appRelativeVirtualPath;
  74. #endif
  75. #region Constructor
  76. protected TemplateControl ()
  77. {
  78. #if NET_2_0
  79. TemplateControl = this;
  80. #endif
  81. Construct ();
  82. }
  83. #endregion
  84. #region Properties
  85. [EditorBrowsable (EditorBrowsableState.Never)]
  86. #if NET_2_0
  87. [Obsolete]
  88. #endif
  89. protected virtual int AutoHandlers {
  90. get { return 0; }
  91. set { }
  92. }
  93. [EditorBrowsable (EditorBrowsableState.Never)]
  94. protected virtual bool SupportAutoEvents {
  95. get { return true; }
  96. }
  97. #if NET_2_0
  98. public string AppRelativeVirtualPath {
  99. get { return _appRelativeVirtualPath; }
  100. set { _appRelativeVirtualPath = value; }
  101. }
  102. #endif
  103. #endregion
  104. #region Methods
  105. protected virtual void Construct ()
  106. {
  107. }
  108. [MonoTODO ("Not implemented")]
  109. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  110. int size,
  111. bool fAsciiOnly)
  112. {
  113. return null;
  114. }
  115. class EvtInfo {
  116. public MethodInfo method;
  117. public string methodName;
  118. public EventInfo evt;
  119. public bool noParams;
  120. }
  121. static Hashtable auto_event_info;
  122. static object auto_event_info_monitor = new Object ();
  123. internal void WireupAutomaticEvents ()
  124. {
  125. if (!SupportAutoEvents || !AutoEventWireup)
  126. return;
  127. ArrayList events = null;
  128. /* Avoid expensive reflection operations by computing the event info only once */
  129. lock (auto_event_info_monitor) {
  130. if (auto_event_info == null)
  131. auto_event_info = new Hashtable ();
  132. events = (ArrayList)auto_event_info [GetType ()];
  133. if (events == null) {
  134. events = CollectAutomaticEventInfo ();
  135. auto_event_info [GetType ()] = events;
  136. }
  137. }
  138. for (int i = 0; i < events.Count; ++i) {
  139. EvtInfo evinfo = (EvtInfo)events [i];
  140. if (evinfo.noParams) {
  141. NoParamsInvoker npi = new NoParamsInvoker (this, evinfo.method);
  142. evinfo.evt.AddEventHandler (this, npi.FakeDelegate);
  143. } else {
  144. evinfo.evt.AddEventHandler (this, Delegate.CreateDelegate (
  145. #if NET_2_0
  146. typeof (EventHandler), this, evinfo.method));
  147. #else
  148. typeof (EventHandler), this, evinfo.methodName));
  149. #endif
  150. }
  151. }
  152. }
  153. private ArrayList CollectAutomaticEventInfo () {
  154. ArrayList events = new ArrayList ();
  155. foreach (string methodName in methodNames) {
  156. MethodInfo method = null;
  157. Type type;
  158. for (type = GetType (); type.Assembly != _System_Web_Assembly; type = type.BaseType) {
  159. method = type.GetMethod (methodName, bflags);
  160. if (method != null)
  161. break;
  162. }
  163. if (method == null)
  164. continue;
  165. if (method.DeclaringType != type) {
  166. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  167. !method.IsFamilyAndAssembly && !method.IsFamily)
  168. continue;
  169. }
  170. if (method.ReturnType != typeof (void))
  171. continue;
  172. ParameterInfo [] parms = method.GetParameters ();
  173. int length = parms.Length;
  174. bool noParams = (length == 0);
  175. if (!noParams && (length != 2 ||
  176. parms [0].ParameterType != typeof (object) ||
  177. parms [1].ParameterType != typeof (EventArgs)))
  178. continue;
  179. int pos = methodName.IndexOf ("_");
  180. string eventName = methodName.Substring (pos + 1);
  181. EventInfo evt = type.GetEvent (eventName);
  182. if (evt == null) {
  183. /* This should never happen */
  184. continue;
  185. }
  186. EvtInfo evinfo = new EvtInfo ();
  187. evinfo.method = method;
  188. evinfo.methodName = methodName;
  189. evinfo.evt = evt;
  190. evinfo.noParams = noParams;
  191. events.Add (evinfo);
  192. }
  193. return events;
  194. }
  195. [EditorBrowsable (EditorBrowsableState.Never)]
  196. protected virtual void FrameworkInitialize ()
  197. {
  198. }
  199. Type GetTypeFromControlPath (string virtualPath)
  200. {
  201. if (virtualPath == null)
  202. throw new ArgumentNullException ("virtualPath");
  203. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  204. #if NET_2_0
  205. return BuildManager.GetCompiledType (vpath);
  206. #else
  207. string realpath = Context.Request.MapPath (vpath);
  208. return UserControlParser.GetCompiledType (vpath, realpath, Context);
  209. #endif
  210. }
  211. public Control LoadControl (string virtualPath)
  212. {
  213. #if NET_2_0
  214. if (virtualPath == null)
  215. throw new ArgumentNullException ("virtualPath");
  216. #else
  217. if (virtualPath == null)
  218. throw new HttpException ("virtualPath is null");
  219. #endif
  220. Type type = GetTypeFromControlPath (virtualPath);
  221. return LoadControl (type, null);
  222. }
  223. public Control LoadControl (Type type, object[] parameters)
  224. {
  225. object [] attrs = null;
  226. if (type != null)
  227. type.GetCustomAttributes (typeof (PartialCachingAttribute), true);
  228. if (attrs != null && attrs.Length == 1) {
  229. PartialCachingAttribute attr = (PartialCachingAttribute) attrs [0];
  230. PartialCachingControl ctrl = new PartialCachingControl (type, parameters);
  231. ctrl.VaryByParams = attr.VaryByParams;
  232. ctrl.VaryByControls = attr.VaryByControls;
  233. ctrl.VaryByCustom = attr.VaryByCustom;
  234. return ctrl;
  235. }
  236. object control = Activator.CreateInstance (type, parameters);
  237. if (control is UserControl)
  238. ((UserControl) control).InitializeAsUserControl (Page);
  239. return (Control) control;
  240. }
  241. public ITemplate LoadTemplate (string virtualPath)
  242. {
  243. #if NET_2_0
  244. if (virtualPath == null)
  245. throw new ArgumentNullException ("virtualPath");
  246. #else
  247. if (virtualPath == null)
  248. throw new HttpException ("virtualPath is null");
  249. #endif
  250. Type t = GetTypeFromControlPath (virtualPath);
  251. return new SimpleTemplate (t);
  252. }
  253. protected virtual void OnAbortTransaction (EventArgs e)
  254. {
  255. EventHandler eh = Events [abortTransaction] as EventHandler;
  256. if (eh != null)
  257. eh (this, e);
  258. }
  259. protected virtual void OnCommitTransaction (EventArgs e)
  260. {
  261. EventHandler eh = Events [commitTransaction] as EventHandler;
  262. if (eh != null)
  263. eh (this, e);
  264. }
  265. protected virtual void OnError (EventArgs e)
  266. {
  267. EventHandler eh = Events [error] as EventHandler;
  268. if (eh != null)
  269. eh (this, e);
  270. }
  271. #if !NET_2_0
  272. [MonoTODO ("Not implemented, always returns null")]
  273. #endif
  274. public Control ParseControl (string content)
  275. {
  276. if (content == null)
  277. throw new ArgumentNullException ("content");
  278. #if NET_2_0
  279. TextReader reader = new StringReader (content);
  280. Type control = UserControlParser.GetCompiledType (reader, HttpContext.Current);
  281. if (control == null)
  282. return null;
  283. return Activator.CreateInstance (control, null) as Control;
  284. #else
  285. return null;
  286. #endif
  287. }
  288. #if NET_2_0
  289. [MonoTODO ("Parser filters not implemented yet. Calls ParseControl (string) for now.")]
  290. public Control ParseControl (string content, bool ignoreParserFilter)
  291. {
  292. return ParseControl (content);
  293. }
  294. #endif
  295. [EditorBrowsable (EditorBrowsableState.Never)]
  296. public
  297. #if !NET_2_0
  298. static
  299. #endif
  300. object ReadStringResource ()
  301. {
  302. throw new NotSupportedException ();
  303. }
  304. #if NET_2_0
  305. protected object GetGlobalResourceObject (string className, string resourceKey)
  306. {
  307. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  308. }
  309. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  310. {
  311. if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) ||
  312. String.IsNullOrEmpty (className) || objType == null)
  313. return null;
  314. object globalObject = GetGlobalResourceObject (className, resourceKey);
  315. if (globalObject == null)
  316. return null;
  317. TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
  318. if (converter == null || !converter.CanConvertFrom (globalObject.GetType ()))
  319. return null;
  320. return converter.ConvertFrom (globalObject);
  321. }
  322. protected object GetLocalResourceObject (string resourceKey)
  323. {
  324. return HttpContext.GetLocalResourceObject (VirtualPathUtility.ToAbsolute (this.AppRelativeVirtualPath),
  325. resourceKey);
  326. }
  327. protected object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  328. {
  329. if (String.IsNullOrEmpty (resourceKey) || String.IsNullOrEmpty (propName) || objType == null)
  330. return null;
  331. object localObject = GetLocalResourceObject (resourceKey);
  332. if (localObject == null)
  333. return null;
  334. TypeConverter converter = TypeDescriptor.GetProperties (objType) [propName].Converter;
  335. if (converter == null || !converter.CanConvertFrom (localObject.GetType ()))
  336. return null;
  337. return converter.ConvertFrom (localObject);
  338. }
  339. internal override TemplateControl TemplateControlInternal {
  340. get { return this; }
  341. }
  342. #endif
  343. [EditorBrowsable (EditorBrowsableState.Never)]
  344. public static object ReadStringResource (Type t)
  345. {
  346. throw new NotSupportedException ();
  347. }
  348. [MonoTODO ("Not implemented, does nothing")]
  349. [EditorBrowsable (EditorBrowsableState.Never)]
  350. protected void SetStringResourcePointer (object stringResourcePointer,
  351. int maxResourceOffset)
  352. {
  353. }
  354. [MonoTODO ("Not implemented, does nothing")]
  355. [EditorBrowsable (EditorBrowsableState.Never)]
  356. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  357. int size, bool fAsciiOnly)
  358. {
  359. }
  360. #endregion
  361. #region Events
  362. [WebSysDescription ("Raised when the user aborts a transaction.")]
  363. public event EventHandler AbortTransaction {
  364. add { Events.AddHandler (abortTransaction, value); }
  365. remove { Events.RemoveHandler (abortTransaction, value); }
  366. }
  367. [WebSysDescription ("Raised when the user initiates a transaction.")]
  368. public event EventHandler CommitTransaction {
  369. add { Events.AddHandler (commitTransaction, value); }
  370. remove { Events.RemoveHandler (commitTransaction, value); }
  371. }
  372. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  373. public event EventHandler Error {
  374. add { Events.AddHandler (error, value); }
  375. remove { Events.RemoveHandler (error, value); }
  376. }
  377. #endregion
  378. class SimpleTemplate : ITemplate
  379. {
  380. Type type;
  381. public SimpleTemplate (Type type)
  382. {
  383. this.type = type;
  384. }
  385. public void InstantiateIn (Control control)
  386. {
  387. Control template = Activator.CreateInstance (type) as Control;
  388. template.SetBindingContainer (false);
  389. control.Controls.Add (template);
  390. }
  391. }
  392. #if NET_2_0
  393. protected internal object Eval (string expression)
  394. {
  395. return DataBinder.Eval (Page.GetDataItem(), expression);
  396. }
  397. protected internal string Eval (string expression, string format)
  398. {
  399. return DataBinder.Eval (Page.GetDataItem(), expression, format);
  400. }
  401. protected internal object XPath (string xpathexpression)
  402. {
  403. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression);
  404. }
  405. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  406. {
  407. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  408. }
  409. protected internal string XPath (string xpathexpression, string format)
  410. {
  411. return XPathBinder.Eval (Page.GetDataItem(), xpathexpression, format);
  412. }
  413. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  414. {
  415. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  416. }
  417. protected internal IEnumerable XPathSelect (string xpathexpression)
  418. {
  419. return XPathBinder.Select (Page.GetDataItem(), xpathexpression);
  420. }
  421. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  422. {
  423. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  424. }
  425. // IFilterResolutionService
  426. [MonoTODO ("Not implemented")]
  427. int IFilterResolutionService.CompareFilters (string filter1, string filter2)
  428. {
  429. throw new NotImplementedException ();
  430. }
  431. [MonoTODO ("Not implemented")]
  432. bool IFilterResolutionService.EvaluateFilter (string filterName)
  433. {
  434. throw new NotImplementedException ();
  435. }
  436. #endif
  437. }
  438. }