UserControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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
  54. #endif
  55. {
  56. private bool initialized;
  57. private AttributeCollection attributes;
  58. private StateBag attrBag;
  59. public UserControl ()
  60. {
  61. }
  62. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  63. [Browsable (false)]
  64. public HttpApplicationState Application
  65. {
  66. get {
  67. Page p = Page;
  68. if (p == null)
  69. return null;
  70. return p.Application;
  71. }
  72. }
  73. private void EnsureAttributes ()
  74. {
  75. if (attributes == null) {
  76. attrBag = new StateBag (true);
  77. if (IsTrackingViewState)
  78. attrBag.TrackViewState ();
  79. attributes = new AttributeCollection (attrBag);
  80. }
  81. }
  82. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  83. [Browsable (false)]
  84. public AttributeCollection Attributes
  85. {
  86. get {
  87. EnsureAttributes ();
  88. return attributes;
  89. }
  90. }
  91. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  92. [Browsable (false)]
  93. public Cache Cache
  94. {
  95. get {
  96. Page p = Page;
  97. if (p == null)
  98. return null;
  99. return p.Cache;
  100. }
  101. }
  102. #if NET_2_0
  103. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  104. [Browsable (false)]
  105. public ControlCachePolicy CachePolicy
  106. {
  107. get {
  108. throw new NotImplementedException ();
  109. }
  110. }
  111. #endif
  112. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  113. [Browsable (false)]
  114. public bool IsPostBack
  115. {
  116. get {
  117. Page p = Page;
  118. if (p == null)
  119. return false;
  120. return p.IsPostBack;
  121. }
  122. }
  123. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  124. [Browsable (false)]
  125. public HttpRequest Request
  126. {
  127. get {
  128. Page p = Page;
  129. if (p == null)
  130. return null;
  131. return p.Request;
  132. }
  133. }
  134. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  135. [Browsable (false)]
  136. public HttpResponse Response
  137. {
  138. get {
  139. Page p = Page;
  140. if (p == null)
  141. return null;
  142. return p.Response;
  143. }
  144. }
  145. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  146. [Browsable (false)]
  147. public HttpServerUtility Server
  148. {
  149. get {
  150. Page p = Page;
  151. if (p == null)
  152. return null;
  153. return p.Server;
  154. }
  155. }
  156. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  157. [Browsable (false)]
  158. public HttpSessionState Session
  159. {
  160. get {
  161. Page p = Page;
  162. if (p == null)
  163. return null;
  164. return p.Session;
  165. }
  166. }
  167. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  168. [Browsable (false)]
  169. public TraceContext Trace
  170. {
  171. get {
  172. Page p = Page;
  173. if (p == null)
  174. return null;
  175. return p.Trace;
  176. }
  177. }
  178. [EditorBrowsable (EditorBrowsableState.Never)]
  179. public void DesignerInitialize ()
  180. {
  181. InitRecursive (null);
  182. }
  183. [EditorBrowsable (EditorBrowsableState.Never)]
  184. public void InitializeAsUserControl (Page page)
  185. {
  186. if (initialized)
  187. return;
  188. this.Page = page;
  189. InitializeAsUserControlInternal ();
  190. }
  191. internal void InitializeAsUserControlInternal ()
  192. {
  193. if (initialized)
  194. return;
  195. initialized = true;
  196. WireupAutomaticEvents ();
  197. FrameworkInitialize ();
  198. }
  199. public string MapPath (string virtualPath)
  200. {
  201. return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
  202. }
  203. protected override void LoadViewState (object savedState)
  204. {
  205. if (savedState != null) {
  206. Pair p = (Pair) savedState;
  207. base.LoadViewState (p.First);
  208. if (p.Second != null) {
  209. EnsureAttributes ();
  210. attrBag.LoadViewState (p.Second);
  211. }
  212. }
  213. }
  214. #if NET_2_0
  215. protected internal
  216. #else
  217. protected
  218. #endif
  219. override void OnInit (EventArgs e)
  220. {
  221. InitializeAsUserControl (Page);
  222. base.OnInit(e);
  223. }
  224. protected override object SaveViewState ()
  225. {
  226. object baseState = base.SaveViewState();
  227. object attrState = null;
  228. if (attributes != null)
  229. attrState = attrBag.SaveViewState ();
  230. if (baseState == null && attrState == null)
  231. return null;
  232. return new Pair (baseState, attrState);
  233. }
  234. string IAttributeAccessor.GetAttribute (string name)
  235. {
  236. if (attributes == null)
  237. return null;
  238. return attributes [name];
  239. }
  240. void IAttributeAccessor.SetAttribute (string name, string value)
  241. {
  242. EnsureAttributes ();
  243. Attributes [name] = value;
  244. }
  245. string IUserControlDesignerAccessor.InnerText
  246. {
  247. get {
  248. string innerText = ((string) ViewState["!DesignTimeInnerText"]);
  249. if (innerText == null)
  250. return string.Empty;
  251. return innerText;
  252. }
  253. set { ViewState["!DesignTimeInnerText"] = value; }
  254. }
  255. string IUserControlDesignerAccessor.TagName
  256. {
  257. get {
  258. string innerTag = ((string) ViewState["!DesignTimeTagName"]);
  259. if (innerTag == null)
  260. return string.Empty;
  261. return innerTag;
  262. }
  263. set { ViewState["!DesignTimeTagName"] = value; }
  264. }
  265. #if NET_2_0
  266. [MonoTODO]
  267. int IFilterResolutionService.CompareFilters (string filter1, string filter2)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. [MonoTODO]
  272. bool IFilterResolutionService.EvaluateFilter (string filterName)
  273. {
  274. throw new NotImplementedException ();
  275. }
  276. #endif
  277. }
  278. }