UserControl.cs 6.4 KB

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