TemplateControl.cs 9.3 KB

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