UserControl.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // System.Web.UI.UserControl.cs
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.ComponentModel.Design.Serialization;
  33. using System.Security.Permissions;
  34. using System.Web.Caching;
  35. using System.Web.SessionState;
  36. namespace System.Web.UI {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. // attributes
  41. [ControlBuilder (typeof (UserControlControlBuilder))]
  42. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  43. [ToolboxItem (false)]
  44. [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  45. [ParseChildren (true)]
  46. #if NET_2_0
  47. [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
  48. #else
  49. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  50. #endif
  51. public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
  52. #if NET_2_0
  53. , INamingContainer, IFilterResolutionService, INonBindingContainer
  54. #endif
  55. {
  56. #if NET_2_0
  57. ControlCachePolicy cachePolicy;
  58. #endif
  59. bool initialized;
  60. AttributeCollection attributes;
  61. StateBag attrBag;
  62. public UserControl ()
  63. {
  64. }
  65. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  66. [Browsable (false)]
  67. public HttpApplicationState Application
  68. {
  69. get {
  70. Page p = Page;
  71. if (p == null)
  72. return null;
  73. return p.Application;
  74. }
  75. }
  76. void EnsureAttributes ()
  77. {
  78. if (attributes == null) {
  79. attrBag = new StateBag (true);
  80. if (IsTrackingViewState)
  81. attrBag.TrackViewState ();
  82. attributes = new AttributeCollection (attrBag);
  83. }
  84. }
  85. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  86. [Browsable (false)]
  87. public AttributeCollection Attributes
  88. {
  89. get {
  90. EnsureAttributes ();
  91. return attributes;
  92. }
  93. }
  94. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  95. [Browsable (false)]
  96. public Cache Cache
  97. {
  98. get {
  99. Page p = Page;
  100. if (p == null)
  101. return null;
  102. return p.Cache;
  103. }
  104. }
  105. #if NET_2_0
  106. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  107. [Browsable (false)]
  108. public ControlCachePolicy CachePolicy
  109. {
  110. get {
  111. BasePartialCachingControl bpcc = Parent as BasePartialCachingControl;
  112. if (bpcc != null)
  113. return bpcc.CachePolicy;
  114. if (cachePolicy == null)
  115. cachePolicy = new ControlCachePolicy ();
  116. return cachePolicy;
  117. }
  118. }
  119. #endif
  120. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  121. [Browsable (false)]
  122. public bool IsPostBack
  123. {
  124. get {
  125. Page p = Page;
  126. if (p == null)
  127. return false;
  128. return p.IsPostBack;
  129. }
  130. }
  131. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  132. [Browsable (false)]
  133. public HttpRequest Request
  134. {
  135. get {
  136. Page p = Page;
  137. if (p == null)
  138. return null;
  139. return p.Request;
  140. }
  141. }
  142. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  143. [Browsable (false)]
  144. public HttpResponse Response
  145. {
  146. get {
  147. Page p = Page;
  148. if (p == null)
  149. return null;
  150. return p.Response;
  151. }
  152. }
  153. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  154. [Browsable (false)]
  155. public HttpServerUtility Server
  156. {
  157. get {
  158. Page p = Page;
  159. if (p == null)
  160. return null;
  161. return p.Server;
  162. }
  163. }
  164. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  165. [Browsable (false)]
  166. public HttpSessionState Session
  167. {
  168. get {
  169. Page p = Page;
  170. if (p == null)
  171. return null;
  172. return p.Session;
  173. }
  174. }
  175. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  176. [Browsable (false)]
  177. public TraceContext Trace
  178. {
  179. get {
  180. Page p = Page;
  181. if (p == null)
  182. return null;
  183. return p.Trace;
  184. }
  185. }
  186. [EditorBrowsable (EditorBrowsableState.Never)]
  187. public void DesignerInitialize ()
  188. {
  189. InitRecursive (null);
  190. }
  191. [EditorBrowsable (EditorBrowsableState.Never)]
  192. public void InitializeAsUserControl (Page page)
  193. {
  194. if (initialized)
  195. return;
  196. this.Page = page;
  197. InitializeAsUserControlInternal ();
  198. }
  199. internal void InitializeAsUserControlInternal ()
  200. {
  201. if (initialized)
  202. return;
  203. initialized = true;
  204. WireupAutomaticEvents ();
  205. FrameworkInitialize ();
  206. }
  207. public string MapPath (string virtualPath)
  208. {
  209. return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
  210. }
  211. protected override void LoadViewState (object savedState)
  212. {
  213. if (savedState != null) {
  214. Pair p = (Pair) savedState;
  215. base.LoadViewState (p.First);
  216. if (p.Second != null) {
  217. EnsureAttributes ();
  218. attrBag.LoadViewState (p.Second);
  219. }
  220. }
  221. }
  222. #if NET_2_0
  223. protected internal
  224. #else
  225. protected
  226. #endif
  227. override void OnInit (EventArgs e)
  228. {
  229. InitializeAsUserControl (Page);
  230. base.OnInit(e);
  231. }
  232. protected override object SaveViewState ()
  233. {
  234. object baseState = base.SaveViewState();
  235. object attrState = null;
  236. if (attributes != null)
  237. attrState = attrBag.SaveViewState ();
  238. if (baseState == null && attrState == null)
  239. return null;
  240. return new Pair (baseState, attrState);
  241. }
  242. string IAttributeAccessor.GetAttribute (string name)
  243. {
  244. if (attributes == null)
  245. return null;
  246. return attributes [name];
  247. }
  248. void IAttributeAccessor.SetAttribute (string name, string value)
  249. {
  250. EnsureAttributes ();
  251. Attributes [name] = value;
  252. }
  253. string IUserControlDesignerAccessor.InnerText
  254. {
  255. get {
  256. string innerText = ((string) ViewState["!DesignTimeInnerText"]);
  257. if (innerText == null)
  258. return string.Empty;
  259. return innerText;
  260. }
  261. set { ViewState["!DesignTimeInnerText"] = value; }
  262. }
  263. string IUserControlDesignerAccessor.TagName
  264. {
  265. get {
  266. string innerTag = ((string) ViewState["!DesignTimeTagName"]);
  267. if (innerTag == null)
  268. return string.Empty;
  269. return innerTag;
  270. }
  271. set { ViewState["!DesignTimeTagName"] = value; }
  272. }
  273. #if NET_2_0
  274. [MonoTODO ("Not implemented")]
  275. int IFilterResolutionService.CompareFilters (string filter1, string filter2)
  276. {
  277. throw new NotImplementedException ();
  278. }
  279. [MonoTODO ("Not implemented")]
  280. bool IFilterResolutionService.EvaluateFilter (string filterName)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. #endif
  285. }
  286. }