UpdatePanel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // UpdatePanel.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich <[email protected]>
  6. //
  7. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.ComponentModel;
  33. using System.Security.Permissions;
  34. namespace System.Web.UI
  35. {
  36. [DesignerAttribute ("System.Web.UI.Design.UpdatePanelDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  37. [DefaultPropertyAttribute ("Triggers")]
  38. [ParseChildrenAttribute (true)]
  39. [PersistChildrenAttribute (false)]
  40. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public class UpdatePanel : Control
  43. {
  44. ITemplate _contentTemplate;
  45. Control _contentTemplateContainer;
  46. UpdatePanelUpdateMode _updateMode = UpdatePanelUpdateMode.Always;
  47. bool _childrenAsTriggers = true;
  48. bool _requiresUpdate = false;
  49. UpdatePanelTriggerCollection _triggers;
  50. [Category ("Behavior")]
  51. [DefaultValue (true)]
  52. public bool ChildrenAsTriggers {
  53. get {
  54. return _childrenAsTriggers;
  55. }
  56. set {
  57. _childrenAsTriggers = value;
  58. }
  59. }
  60. [TemplateInstance (TemplateInstance.Single)]
  61. [PersistenceMode (PersistenceMode.InnerProperty)]
  62. [Browsable (false)]
  63. public ITemplate ContentTemplate {
  64. get {
  65. return _contentTemplate;
  66. }
  67. set {
  68. _contentTemplate = value;
  69. }
  70. }
  71. [Browsable (false)]
  72. public Control ContentTemplateContainer {
  73. get {
  74. if (_contentTemplateContainer == null) {
  75. _contentTemplateContainer = CreateContentTemplateContainer ();
  76. Controls.Add (_contentTemplateContainer);
  77. }
  78. return _contentTemplateContainer;
  79. }
  80. }
  81. public override sealed ControlCollection Controls {
  82. get {
  83. return base.Controls;
  84. }
  85. }
  86. [Browsable (false)]
  87. public bool IsInPartialRendering {
  88. get {
  89. throw new NotImplementedException ();
  90. }
  91. }
  92. [Category ("Layout")]
  93. public UpdatePanelRenderMode RenderMode {
  94. get {
  95. throw new NotImplementedException ();
  96. }
  97. set {
  98. throw new NotImplementedException ();
  99. }
  100. }
  101. protected internal virtual bool RequiresUpdate {
  102. get {
  103. return UpdateMode == UpdatePanelUpdateMode.Always || _requiresUpdate;
  104. }
  105. }
  106. internal ScriptManager ScriptManager {
  107. get {
  108. ScriptManager manager = ScriptManager.GetCurrent (Page);
  109. if (manager == null)
  110. throw new InvalidOperationException (String.Format ("The control with ID '{0}' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.", ID));
  111. return manager;
  112. }
  113. }
  114. [MergableProperty (false)]
  115. [DefaultValue ("")]
  116. [PersistenceMode (PersistenceMode.InnerProperty)]
  117. [Category ("Behavior")]
  118. public UpdatePanelTriggerCollection Triggers {
  119. get {
  120. if (_triggers == null)
  121. _triggers = new UpdatePanelTriggerCollection (this);
  122. return _triggers;
  123. }
  124. }
  125. [Category ("Behavior")]
  126. [DefaultValueAttribute(UpdatePanelUpdateMode.Always)]
  127. public UpdatePanelUpdateMode UpdateMode {
  128. get {
  129. return _updateMode;
  130. }
  131. set {
  132. _updateMode = value;
  133. }
  134. }
  135. protected virtual Control CreateContentTemplateContainer ()
  136. {
  137. return new Control ();
  138. }
  139. [MonoTODO()]
  140. protected override sealed ControlCollection CreateControlCollection ()
  141. {
  142. // TODO: Because this method is protected and sealed, it is visible to classes that inherit
  143. // from the UpdatePanel class, but it cannot be overridden. This method overrides
  144. // the base implementation to return a specialized ControlCollection object that throws
  145. // an InvalidOperationException when the Add(Control), AddAt(Int32, Control), Clear(),
  146. // Remove(Control), or RemoveAt(Int32) method of the ControlCollection class is invoked.
  147. // To change the content of the UpdatePanel control, modify the child controls of
  148. // the ContentTemplateContainer property.
  149. return base.CreateControlCollection();
  150. }
  151. protected internal virtual void Initialize ()
  152. {
  153. if (_triggers != null) {
  154. for (int i = 0; i < _triggers.Count; i++) {
  155. _triggers [i].Initialize ();
  156. }
  157. }
  158. }
  159. protected override void OnInit (EventArgs e)
  160. {
  161. base.OnInit (e);
  162. ScriptManager.RegisterUpdatePanel (this);
  163. if (ContentTemplate != null)
  164. ContentTemplate.InstantiateIn (ContentTemplateContainer);
  165. }
  166. protected override void OnLoad (EventArgs e)
  167. {
  168. base.OnLoad (e);
  169. Initialize ();
  170. }
  171. protected override void OnPreRender (EventArgs e)
  172. {
  173. base.OnPreRender (e);
  174. if (UpdateMode == UpdatePanelUpdateMode.Always && !ChildrenAsTriggers)
  175. throw new InvalidOperationException (String.Format ("ChildrenAsTriggers cannot be set to false when UpdateMode is set to Always on UpdatePanel '{0}'", ID));
  176. }
  177. protected override void OnUnload (EventArgs e)
  178. {
  179. base.OnUnload (e);
  180. }
  181. protected override void Render (HtmlTextWriter writer)
  182. {
  183. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  184. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  185. RenderChildren (writer);
  186. writer.RenderEndTag ();
  187. }
  188. protected override void RenderChildren (HtmlTextWriter writer)
  189. {
  190. if (ScriptManager.IsInAsyncPostBack)
  191. return;
  192. RenderChildrenInternal (writer);
  193. }
  194. internal void RenderChildrenInternal (HtmlTextWriter writer) {
  195. base.RenderChildren (writer);
  196. }
  197. public void Update ()
  198. {
  199. _requiresUpdate = true;
  200. }
  201. }
  202. }