UpdatePanel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. using System.IO;
  35. namespace System.Web.UI
  36. {
  37. [DesignerAttribute ("System.Web.UI.Design.UpdatePanelDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  38. [DefaultPropertyAttribute ("Triggers")]
  39. [ParseChildrenAttribute (true)]
  40. [PersistChildrenAttribute (false)]
  41. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. public class UpdatePanel : Control
  44. {
  45. ITemplate _contentTemplate;
  46. Control _contentTemplateContainer;
  47. UpdatePanelUpdateMode _updateMode = UpdatePanelUpdateMode.Always;
  48. bool _childrenAsTriggers = true;
  49. bool _requiresUpdate = false;
  50. UpdatePanelTriggerCollection _triggers;
  51. UpdatePanelRenderMode _renderMode = UpdatePanelRenderMode.Block;
  52. ScriptManager _scriptManager;
  53. [Category ("Behavior")]
  54. [DefaultValue (true)]
  55. public bool ChildrenAsTriggers {
  56. get {
  57. return _childrenAsTriggers;
  58. }
  59. set {
  60. _childrenAsTriggers = value;
  61. }
  62. }
  63. [TemplateInstance (TemplateInstance.Single)]
  64. [PersistenceMode (PersistenceMode.InnerProperty)]
  65. [Browsable (false)]
  66. public ITemplate ContentTemplate {
  67. get {
  68. return _contentTemplate;
  69. }
  70. set {
  71. _contentTemplate = value;
  72. }
  73. }
  74. [Browsable (false)]
  75. public Control ContentTemplateContainer {
  76. get {
  77. if (_contentTemplateContainer == null) {
  78. _contentTemplateContainer = CreateContentTemplateContainer ();
  79. Controls.Add (_contentTemplateContainer);
  80. }
  81. return _contentTemplateContainer;
  82. }
  83. }
  84. public override sealed ControlCollection Controls {
  85. get {
  86. return base.Controls;
  87. }
  88. }
  89. [Browsable (false)]
  90. public bool IsInPartialRendering {
  91. get {
  92. return ScriptManager.IsInPartialRendering;
  93. }
  94. }
  95. [Category ("Layout")]
  96. public UpdatePanelRenderMode RenderMode {
  97. get {
  98. return _renderMode;
  99. }
  100. set {
  101. _renderMode = value;
  102. }
  103. }
  104. protected internal virtual bool RequiresUpdate {
  105. get {
  106. return UpdateMode == UpdatePanelUpdateMode.Always || _requiresUpdate;
  107. }
  108. }
  109. internal ScriptManager ScriptManager {
  110. get {
  111. if (_scriptManager == null) {
  112. _scriptManager = ScriptManager.GetCurrent (Page);
  113. if (_scriptManager == null)
  114. 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));
  115. }
  116. return _scriptManager;
  117. }
  118. }
  119. [MergableProperty (false)]
  120. [DefaultValue ("")]
  121. [PersistenceMode (PersistenceMode.InnerProperty)]
  122. [Category ("Behavior")]
  123. public UpdatePanelTriggerCollection Triggers {
  124. get {
  125. if (_triggers == null)
  126. _triggers = new UpdatePanelTriggerCollection (this);
  127. return _triggers;
  128. }
  129. }
  130. [Category ("Behavior")]
  131. [DefaultValueAttribute (UpdatePanelUpdateMode.Always)]
  132. public UpdatePanelUpdateMode UpdateMode {
  133. get {
  134. return _updateMode;
  135. }
  136. set {
  137. _updateMode = value;
  138. }
  139. }
  140. protected virtual Control CreateContentTemplateContainer () {
  141. return new Control ();
  142. }
  143. [MonoTODO ()]
  144. protected override sealed ControlCollection CreateControlCollection () {
  145. // TODO: Because this method is protected and sealed, it is visible to classes that inherit
  146. // from the UpdatePanel class, but it cannot be overridden. This method overrides
  147. // the base implementation to return a specialized ControlCollection object that throws
  148. // an InvalidOperationException when the Add(Control), AddAt(Int32, Control), Clear(),
  149. // Remove(Control), or RemoveAt(Int32) method of the ControlCollection class is invoked.
  150. // To change the content of the UpdatePanel control, modify the child controls of
  151. // the ContentTemplateContainer property.
  152. return base.CreateControlCollection ();
  153. }
  154. protected internal virtual void Initialize () {
  155. if (_triggers != null) {
  156. for (int i = 0; i < _triggers.Count; i++) {
  157. _triggers [i].Initialize ();
  158. }
  159. }
  160. }
  161. protected override void OnInit (EventArgs e) {
  162. base.OnInit (e);
  163. ScriptManager.RegisterUpdatePanel (this);
  164. if (ContentTemplate != null)
  165. ContentTemplate.InstantiateIn (ContentTemplateContainer);
  166. }
  167. protected override void OnLoad (EventArgs e) {
  168. base.OnLoad (e);
  169. Initialize ();
  170. }
  171. protected override void OnPreRender (EventArgs e) {
  172. base.OnPreRender (e);
  173. if (UpdateMode == UpdatePanelUpdateMode.Always && !ChildrenAsTriggers)
  174. throw new InvalidOperationException (String.Format ("ChildrenAsTriggers cannot be set to false when UpdateMode is set to Always on UpdatePanel '{0}'", ID));
  175. }
  176. protected override void OnUnload (EventArgs e) {
  177. base.OnUnload (e);
  178. }
  179. protected override void Render (HtmlTextWriter writer) {
  180. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  181. if (RenderMode == UpdatePanelRenderMode.Block)
  182. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  183. else
  184. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  185. RenderChildren (writer);
  186. writer.RenderEndTag ();
  187. }
  188. protected override void RenderChildren (HtmlTextWriter writer) {
  189. if (ScriptManager.IsInAsyncPostBack){
  190. if (!ScriptManager.IsInPartialRendering) {
  191. ScriptManager.IsInPartialRendering = true;
  192. ScriptManager.AlternativeHtmlTextWriter altWriter = writer as ScriptManager.AlternativeHtmlTextWriter;
  193. if (altWriter == null)
  194. altWriter = writer.InnerWriter as ScriptManager.AlternativeHtmlTextWriter;
  195. if (altWriter == null)
  196. throw new InvalidOperationException ("Internal error. Invalid writer object.");
  197. HtmlTextWriter responseOutput = altWriter.ResponseOutput;
  198. StringBuilder sb = new StringBuilder ();
  199. HtmlTextWriter w = new HtmlTextWriter (new StringWriter (sb));
  200. base.RenderChildren (w);
  201. w.Flush ();
  202. ScriptManager.WriteCallbackPanel (responseOutput, this, sb);
  203. ScriptManager.IsInPartialRendering = false;
  204. }
  205. else {
  206. if (ScriptManager.IsInPartialRendering)
  207. ScriptManager.RegisterChildUpdatePanel (this);
  208. base.RenderChildren (writer);
  209. }
  210. }
  211. else
  212. base.RenderChildren (writer);
  213. }
  214. public void Update () {
  215. _requiresUpdate = true;
  216. }
  217. }
  218. }