5 // Igor Zelmanovich <igorz@mainsoft.com>
7 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
33 using System.ComponentModel;
34 using System.Security.Permissions;
37 namespace System.Web.UI
39 [DesignerAttribute ("System.Web.UI.Design.UpdatePanelDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
40 [DefaultPropertyAttribute ("Triggers")]
41 [ParseChildrenAttribute (true)]
42 [PersistChildrenAttribute (false)]
43 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public class UpdatePanel : Control
47 ITemplate _contentTemplate;
48 Control _contentTemplateContainer;
49 UpdatePanelUpdateMode _updateMode = UpdatePanelUpdateMode.Always;
50 bool _childrenAsTriggers = true;
51 bool _requiresUpdate = false;
52 UpdatePanelTriggerCollection _triggers;
53 UpdatePanelRenderMode _renderMode = UpdatePanelRenderMode.Block;
54 ScriptManager _scriptManager;
56 [Category ("Behavior")]
58 public bool ChildrenAsTriggers {
60 return _childrenAsTriggers;
63 _childrenAsTriggers = value;
67 [TemplateInstance (TemplateInstance.Single)]
68 [PersistenceMode (PersistenceMode.InnerProperty)]
70 public ITemplate ContentTemplate {
72 return _contentTemplate;
75 _contentTemplate = value;
80 public Control ContentTemplateContainer {
82 if (_contentTemplateContainer == null) {
83 _contentTemplateContainer = CreateContentTemplateContainer ();
84 Controls.Add (_contentTemplateContainer);
86 return _contentTemplateContainer;
90 public override sealed ControlCollection Controls {
97 public bool IsInPartialRendering {
99 return ScriptManager.IsInPartialRendering;
103 [Category ("Layout")]
104 public UpdatePanelRenderMode RenderMode {
113 protected internal virtual bool RequiresUpdate {
115 return UpdateMode == UpdatePanelUpdateMode.Always || _requiresUpdate;
119 internal ScriptManager ScriptManager {
121 if (_scriptManager == null) {
122 _scriptManager = ScriptManager.GetCurrent (Page);
123 if (_scriptManager == null)
124 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));
126 return _scriptManager;
130 [MergableProperty (false)]
132 [PersistenceMode (PersistenceMode.InnerProperty)]
133 [Category ("Behavior")]
134 public UpdatePanelTriggerCollection Triggers {
136 if (_triggers == null)
137 _triggers = new UpdatePanelTriggerCollection (this);
142 [Category ("Behavior")]
143 [DefaultValueAttribute (UpdatePanelUpdateMode.Always)]
144 public UpdatePanelUpdateMode UpdateMode {
153 protected virtual Control CreateContentTemplateContainer () {
154 return new Control ();
158 protected override sealed ControlCollection CreateControlCollection () {
159 // TODO: Because this method is protected and sealed, it is visible to classes that inherit
160 // from the UpdatePanel class, but it cannot be overridden. This method overrides
161 // the base implementation to return a specialized ControlCollection object that throws
162 // an InvalidOperationException when the Add(Control), AddAt(Int32, Control), Clear(),
163 // Remove(Control), or RemoveAt(Int32) method of the ControlCollection class is invoked.
164 // To change the content of the UpdatePanel control, modify the child controls of
165 // the ContentTemplateContainer property.
167 return base.CreateControlCollection ();
170 protected internal virtual void Initialize () {
171 if (_triggers != null) {
172 for (int i = 0; i < _triggers.Count; i++) {
173 _triggers [i].Initialize ();
178 protected internal override void OnInit (EventArgs e) {
181 ScriptManager.RegisterUpdatePanel (this);
183 if (ContentTemplate != null)
184 ContentTemplate.InstantiateIn (ContentTemplateContainer);
187 protected internal override void OnLoad (EventArgs e) {
193 protected internal override void OnPreRender (EventArgs e) {
194 base.OnPreRender (e);
196 if (UpdateMode == UpdatePanelUpdateMode.Always && !ChildrenAsTriggers)
197 throw new InvalidOperationException (String.Format ("ChildrenAsTriggers cannot be set to false when UpdateMode is set to Always on UpdatePanel '{0}'", ID));
200 protected internal override void OnUnload (EventArgs e) {
204 protected internal override void Render (HtmlTextWriter writer) {
205 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
206 if (RenderMode == UpdatePanelRenderMode.Block)
207 writer.RenderBeginTag (HtmlTextWriterTag.Div);
209 writer.RenderBeginTag (HtmlTextWriterTag.Span);
210 RenderChildren (writer);
211 writer.RenderEndTag ();
214 protected internal override void RenderChildren (HtmlTextWriter writer) {
215 if (ScriptManager.IsInAsyncPostBack){
216 if (RequiresUpdate && !ScriptManager.IsInPartialRendering) {
217 ScriptManager.IsInPartialRendering = true;
218 HtmlTextWriter responseOutput = ((ScriptManager.AlternativeHtmlTextWriter) writer).ResponseOutput;
219 StringBuilder sb = new StringBuilder ();
220 HtmlTextWriter w = new HtmlTextWriter (new StringWriter (sb));
221 base.RenderChildren (w);
224 ScriptManager.WriteCallbackPanel (responseOutput, this, sb);
226 ScriptManager.IsInPartialRendering = false;
229 if (ScriptManager.IsInPartialRendering)
230 ScriptManager.RegisterChildUpdatePanel (this);
231 base.RenderChildren (writer);
235 base.RenderChildren (writer);
238 public void Update () {
239 _requiresUpdate = true;