UserControl.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.Web.UI.UserControl
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Web.Caching;
  11. using System.Web.SessionState;
  12. namespace System.Web.UI
  13. {
  14. public class UserControl : TemplateControl, IAttributeAccessor
  15. {
  16. private bool initialized;
  17. private AttributeCollection attributes;
  18. private StateBag attrBag;
  19. public UserControl ()
  20. {
  21. //??
  22. }
  23. public HttpApplicationState Application
  24. {
  25. get {
  26. Page p = Page;
  27. if (p == null)
  28. return null;
  29. return p.Application;
  30. }
  31. }
  32. private void EnsureAttributes ()
  33. {
  34. if (attributes == null) {
  35. attrBag = new StateBag (true);
  36. if (IsTrackingViewState)
  37. attrBag.TrackViewState ();
  38. attributes = new AttributeCollection (attrBag);
  39. }
  40. }
  41. public AttributeCollection Attributes
  42. {
  43. get {
  44. return attributes;
  45. }
  46. }
  47. public Cache Cache
  48. {
  49. get {
  50. Page p = Page;
  51. if (p == null)
  52. return null;
  53. return p.Cache;
  54. }
  55. }
  56. public bool IsPostBack
  57. {
  58. get {
  59. Page p = Page;
  60. if (p == null)
  61. return false;
  62. return p.IsPostBack;
  63. }
  64. }
  65. public HttpRequest Request
  66. {
  67. get {
  68. Page p = Page;
  69. if (p == null)
  70. return null;
  71. return p.Request;
  72. }
  73. }
  74. public HttpResponse Response
  75. {
  76. get {
  77. Page p = Page;
  78. if (p == null)
  79. return null;
  80. return p.Response;
  81. }
  82. }
  83. public HttpServerUtility Server
  84. {
  85. get {
  86. Page p = Page;
  87. if (p == null)
  88. return null;
  89. return p.Server;
  90. }
  91. }
  92. public HttpSessionState Session
  93. {
  94. get {
  95. Page p = Page;
  96. if (p == null)
  97. return null;
  98. return p.Session;
  99. }
  100. }
  101. public TraceContext Trace
  102. {
  103. get {
  104. Page p = Page;
  105. if (p == null)
  106. return null;
  107. return p.Trace;
  108. }
  109. }
  110. [MonoTODO]
  111. public void DesignerInitialize ()
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. public void InitializeAsUserControl (Page page)
  116. {
  117. if (initialized)
  118. return;
  119. initialized = true;
  120. WireupAutomaticEvents ();
  121. FrameworkInitialize ();
  122. }
  123. [MonoTODO]
  124. public string MapPath (string virtualPath)
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. protected override void LoadViewState (object savedState)
  129. {
  130. if (savedState != null) {
  131. Pair p = (Pair) savedState;
  132. base.LoadViewState (p.First);
  133. if (p.Second != null) {
  134. EnsureAttributes ();
  135. attrBag.LoadViewState (p.Second);
  136. }
  137. }
  138. }
  139. protected override void OnInit (EventArgs e)
  140. {
  141. if (Page != null)
  142. InitializeAsUserControl (Page);
  143. base.OnInit(e);
  144. }
  145. protected override object SaveViewState ()
  146. {
  147. object baseState = base.SaveViewState();
  148. object attrState = null;
  149. if (attributes != null)
  150. attrState = attrBag.SaveViewState ();
  151. if (baseState == null && attrState == null)
  152. return null;
  153. return new Pair (baseState, attrState);
  154. }
  155. string IAttributeAccessor.GetAttribute (string name)
  156. {
  157. if (attributes == null)
  158. return null;
  159. return attributes [name];
  160. }
  161. void IAttributeAccessor.SetAttribute (string name, string value)
  162. {
  163. EnsureAttributes ();
  164. Attributes [name] = value;
  165. }
  166. }
  167. }