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