UserControl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. //
  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;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design;
  33. using System.ComponentModel.Design.Serialization;
  34. using System.Web.Caching;
  35. using System.Web.SessionState;
  36. namespace System.Web.UI
  37. {
  38. [ControlBuilder (typeof (UserControlControlBuilder))]
  39. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  40. [ToolboxItem (false)]
  41. [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  42. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  43. #if NET_2_0
  44. [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
  45. [ParseChildren (true, "", ChildControlType = typeof (Control))]
  46. #else
  47. [ParseChildren (true)]
  48. #endif
  49. public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
  50. #if NET_2_0
  51. , INamingContainer
  52. #endif
  53. {
  54. private bool initialized;
  55. private AttributeCollection attributes;
  56. private StateBag attrBag;
  57. public UserControl ()
  58. {
  59. //??
  60. }
  61. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  62. [Browsable (false)]
  63. public HttpApplicationState Application
  64. {
  65. get {
  66. Page p = Page;
  67. if (p == null)
  68. return null;
  69. return p.Application;
  70. }
  71. }
  72. private void EnsureAttributes ()
  73. {
  74. if (attributes == null) {
  75. attrBag = new StateBag (true);
  76. if (IsTrackingViewState)
  77. attrBag.TrackViewState ();
  78. attributes = new AttributeCollection (attrBag);
  79. }
  80. }
  81. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  82. [Browsable (false)]
  83. public AttributeCollection Attributes
  84. {
  85. get {
  86. return attributes;
  87. }
  88. }
  89. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  90. [Browsable (false)]
  91. public Cache Cache
  92. {
  93. get {
  94. Page p = Page;
  95. if (p == null)
  96. return null;
  97. return p.Cache;
  98. }
  99. }
  100. #if NET_2_0
  101. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  102. [Browsable (false)]
  103. public ControlCachePolicy CachePolicy
  104. {
  105. get {
  106. throw new NotImplementedException ();
  107. }
  108. }
  109. #endif
  110. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  111. [Browsable (false)]
  112. public bool IsPostBack
  113. {
  114. get {
  115. Page p = Page;
  116. if (p == null)
  117. return false;
  118. return p.IsPostBack;
  119. }
  120. }
  121. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  122. [Browsable (false)]
  123. public HttpRequest Request
  124. {
  125. get {
  126. Page p = Page;
  127. if (p == null)
  128. return null;
  129. return p.Request;
  130. }
  131. }
  132. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  133. [Browsable (false)]
  134. public HttpResponse Response
  135. {
  136. get {
  137. Page p = Page;
  138. if (p == null)
  139. return null;
  140. return p.Response;
  141. }
  142. }
  143. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  144. [Browsable (false)]
  145. public HttpServerUtility Server
  146. {
  147. get {
  148. Page p = Page;
  149. if (p == null)
  150. return null;
  151. return p.Server;
  152. }
  153. }
  154. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  155. [Browsable (false)]
  156. public HttpSessionState Session
  157. {
  158. get {
  159. Page p = Page;
  160. if (p == null)
  161. return null;
  162. return p.Session;
  163. }
  164. }
  165. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  166. [Browsable (false)]
  167. public TraceContext Trace
  168. {
  169. get {
  170. Page p = Page;
  171. if (p == null)
  172. return null;
  173. return p.Trace;
  174. }
  175. }
  176. [EditorBrowsable (EditorBrowsableState.Never)]
  177. public void DesignerInitialize ()
  178. {
  179. InitRecursive (null);
  180. }
  181. [EditorBrowsable (EditorBrowsableState.Never)]
  182. public void InitializeAsUserControl (Page page)
  183. {
  184. if (initialized)
  185. return;
  186. initialized = true;
  187. this.Page = page;
  188. WireupAutomaticEvents ();
  189. FrameworkInitialize ();
  190. }
  191. public string MapPath (string virtualPath)
  192. {
  193. return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
  194. }
  195. protected override void LoadViewState (object savedState)
  196. {
  197. if (savedState != null) {
  198. Pair p = (Pair) savedState;
  199. base.LoadViewState (p.First);
  200. if (p.Second != null) {
  201. EnsureAttributes ();
  202. attrBag.LoadViewState (p.Second);
  203. }
  204. }
  205. }
  206. #if NET_2_0
  207. protected internal
  208. #else
  209. protected
  210. #endif
  211. override void OnInit (EventArgs e)
  212. {
  213. InitializeAsUserControl (Page);
  214. base.OnInit(e);
  215. }
  216. protected override object SaveViewState ()
  217. {
  218. object baseState = base.SaveViewState();
  219. object attrState = null;
  220. if (attributes != null)
  221. attrState = attrBag.SaveViewState ();
  222. if (baseState == null && attrState == null)
  223. return null;
  224. return new Pair (baseState, attrState);
  225. }
  226. string IAttributeAccessor.GetAttribute (string name)
  227. {
  228. if (attributes == null)
  229. return null;
  230. return attributes [name];
  231. }
  232. void IAttributeAccessor.SetAttribute (string name, string value)
  233. {
  234. EnsureAttributes ();
  235. Attributes [name] = value;
  236. }
  237. string IUserControlDesignerAccessor.InnerText
  238. {
  239. get {
  240. string innerText = ((string) ViewState["!DesignTimeInnerText"]);
  241. if (innerText == null)
  242. return string.Empty;
  243. return innerText;
  244. }
  245. set { ViewState["!DesignTimeInnerText"] = value; }
  246. }
  247. string IUserControlDesignerAccessor.TagName
  248. {
  249. get {
  250. string innerTag = ((string) ViewState["!DesignTimeTagName"]);
  251. if (innerTag == null)
  252. return string.Empty;
  253. return innerTag;
  254. }
  255. set { ViewState["!DesignTimeTagName"] = value; }
  256. }
  257. }
  258. }