2
0

UpdatePanel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. [Category ("Behavior")]
  53. [DefaultValue (true)]
  54. public bool ChildrenAsTriggers {
  55. get {
  56. return _childrenAsTriggers;
  57. }
  58. set {
  59. _childrenAsTriggers = value;
  60. }
  61. }
  62. [TemplateInstance (TemplateInstance.Single)]
  63. [PersistenceMode (PersistenceMode.InnerProperty)]
  64. [Browsable (false)]
  65. public ITemplate ContentTemplate {
  66. get {
  67. return _contentTemplate;
  68. }
  69. set {
  70. _contentTemplate = value;
  71. }
  72. }
  73. [Browsable (false)]
  74. public Control ContentTemplateContainer {
  75. get {
  76. if (_contentTemplateContainer == null) {
  77. _contentTemplateContainer = CreateContentTemplateContainer ();
  78. Controls.Add (_contentTemplateContainer);
  79. }
  80. return _contentTemplateContainer;
  81. }
  82. }
  83. public override sealed ControlCollection Controls {
  84. get {
  85. return base.Controls;
  86. }
  87. }
  88. [Browsable (false)]
  89. public bool IsInPartialRendering {
  90. get {
  91. throw new NotImplementedException ();
  92. }
  93. }
  94. [Category ("Layout")]
  95. public UpdatePanelRenderMode RenderMode {
  96. get {
  97. return _renderMode;
  98. }
  99. set {
  100. _renderMode = value;
  101. }
  102. }
  103. protected internal virtual bool RequiresUpdate {
  104. get {
  105. return UpdateMode == UpdatePanelUpdateMode.Always || _requiresUpdate;
  106. }
  107. }
  108. internal ScriptManager ScriptManager {
  109. get {
  110. ScriptManager manager = ScriptManager.GetCurrent (Page);
  111. if (manager == null)
  112. 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));
  113. return manager;
  114. }
  115. }
  116. [MergableProperty (false)]
  117. [DefaultValue ("")]
  118. [PersistenceMode (PersistenceMode.InnerProperty)]
  119. [Category ("Behavior")]
  120. public UpdatePanelTriggerCollection Triggers {
  121. get {
  122. if (_triggers == null)
  123. _triggers = new UpdatePanelTriggerCollection (this);
  124. return _triggers;
  125. }
  126. }
  127. [Category ("Behavior")]
  128. [DefaultValueAttribute (UpdatePanelUpdateMode.Always)]
  129. public UpdatePanelUpdateMode UpdateMode {
  130. get {
  131. return _updateMode;
  132. }
  133. set {
  134. _updateMode = value;
  135. }
  136. }
  137. protected virtual Control CreateContentTemplateContainer () {
  138. return new Control ();
  139. }
  140. [MonoTODO ()]
  141. protected override sealed ControlCollection CreateControlCollection () {
  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. if (_triggers != null) {
  153. for (int i = 0; i < _triggers.Count; i++) {
  154. _triggers [i].Initialize ();
  155. }
  156. }
  157. }
  158. protected override void OnInit (EventArgs e) {
  159. base.OnInit (e);
  160. ScriptManager.RegisterUpdatePanel (this);
  161. if (ContentTemplate != null)
  162. ContentTemplate.InstantiateIn (ContentTemplateContainer);
  163. }
  164. protected override void OnLoad (EventArgs e) {
  165. base.OnLoad (e);
  166. Initialize ();
  167. }
  168. protected override void OnPreRender (EventArgs e) {
  169. base.OnPreRender (e);
  170. if (UpdateMode == UpdatePanelUpdateMode.Always && !ChildrenAsTriggers)
  171. throw new InvalidOperationException (String.Format ("ChildrenAsTriggers cannot be set to false when UpdateMode is set to Always on UpdatePanel '{0}'", ID));
  172. }
  173. protected override void OnUnload (EventArgs e) {
  174. base.OnUnload (e);
  175. }
  176. protected override void Render (HtmlTextWriter writer) {
  177. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  178. if (RenderMode == UpdatePanelRenderMode.Block)
  179. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  180. else
  181. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  182. RenderChildren (writer);
  183. writer.RenderEndTag ();
  184. }
  185. protected override void RenderChildren (HtmlTextWriter writer) {
  186. if (ScriptManager.IsInAsyncPostBack){
  187. if (RequiresUpdate && !ScriptManager.IsInPartialRendering) {
  188. ScriptManager.IsInPartialRendering = true;
  189. HtmlTextWriter responseOutput = ((ScriptManager.AlternativeHtmlTextWriter) writer).ResponseOutput;
  190. StringBuilder sb = new StringBuilder ();
  191. HtmlTextWriter w = new HtmlTextWriter (new StringWriter (sb));
  192. base.RenderChildren (w);
  193. w.Flush ();
  194. ScriptManager.WriteCallbackPanel (responseOutput, this, sb);
  195. for (int i = 0; i < sb.Length; i++)
  196. writer.Write (sb [i]);
  197. ScriptManager.IsInPartialRendering = false;
  198. }
  199. else {
  200. if (ScriptManager.IsInPartialRendering)
  201. ScriptManager.RegisterChildUpdatePanel (this);
  202. base.RenderChildren (writer);
  203. }
  204. }
  205. else
  206. base.RenderChildren (writer);
  207. }
  208. public void Update () {
  209. _requiresUpdate = true;
  210. }
  211. }
  212. }