UserControl.cs 3.2 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. [ControlBuilder (typeof (UserControlControlBuilder))]
  15. public class UserControl : TemplateControl, IAttributeAccessor
  16. {
  17. private bool initialized;
  18. private AttributeCollection attributes;
  19. private StateBag attrBag;
  20. public UserControl ()
  21. {
  22. //??
  23. }
  24. public HttpApplicationState Application
  25. {
  26. get {
  27. Page p = Page;
  28. if (p == null)
  29. return null;
  30. return p.Application;
  31. }
  32. }
  33. private void EnsureAttributes ()
  34. {
  35. if (attributes == null) {
  36. attrBag = new StateBag (true);
  37. if (IsTrackingViewState)
  38. attrBag.TrackViewState ();
  39. attributes = new AttributeCollection (attrBag);
  40. }
  41. }
  42. public AttributeCollection Attributes
  43. {
  44. get {
  45. return attributes;
  46. }
  47. }
  48. public Cache Cache
  49. {
  50. get {
  51. Page p = Page;
  52. if (p == null)
  53. return null;
  54. return p.Cache;
  55. }
  56. }
  57. public bool IsPostBack
  58. {
  59. get {
  60. Page p = Page;
  61. if (p == null)
  62. return false;
  63. return p.IsPostBack;
  64. }
  65. }
  66. public HttpRequest Request
  67. {
  68. get {
  69. Page p = Page;
  70. if (p == null)
  71. return null;
  72. return p.Request;
  73. }
  74. }
  75. public HttpResponse Response
  76. {
  77. get {
  78. Page p = Page;
  79. if (p == null)
  80. return null;
  81. return p.Response;
  82. }
  83. }
  84. public HttpServerUtility Server
  85. {
  86. get {
  87. Page p = Page;
  88. if (p == null)
  89. return null;
  90. return p.Server;
  91. }
  92. }
  93. public HttpSessionState Session
  94. {
  95. get {
  96. Page p = Page;
  97. if (p == null)
  98. return null;
  99. return p.Session;
  100. }
  101. }
  102. public TraceContext Trace
  103. {
  104. get {
  105. Page p = Page;
  106. if (p == null)
  107. return null;
  108. return p.Trace;
  109. }
  110. }
  111. [MonoTODO]
  112. public void DesignerInitialize ()
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. public void InitializeAsUserControl (Page page)
  117. {
  118. if (initialized)
  119. return;
  120. initialized = true;
  121. this.Page = page;
  122. WireupAutomaticEvents ();
  123. FrameworkInitialize ();
  124. }
  125. public string MapPath (string virtualPath)
  126. {
  127. return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
  128. }
  129. protected override void LoadViewState (object savedState)
  130. {
  131. if (savedState != null) {
  132. Pair p = (Pair) savedState;
  133. base.LoadViewState (p.First);
  134. if (p.Second != null) {
  135. EnsureAttributes ();
  136. attrBag.LoadViewState (p.Second);
  137. }
  138. }
  139. }
  140. protected override void OnInit (EventArgs e)
  141. {
  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. }