TemplateControl.cs 10 KB

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