System.Web.UI.WebControls.IDataBoundItemControl from reference source
[mono.git] / mcs / class / System.Web / System.Web.UI / UserControl.cs
1 //
2 // System.Web.UI.UserControl.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.ComponentModel.Design.Serialization;
34 using System.Security.Permissions;
35 using System.Web.Caching;
36 using System.Web.SessionState;
37
38 namespace System.Web.UI
39 {
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         // attributes
44         [ControlBuilder (typeof (UserControlControlBuilder))]
45         [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
46         [ToolboxItem (false)]
47         [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
48         [ParseChildren (true)]
49         [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
50         public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor, INamingContainer, IFilterResolutionService, INonBindingContainer
51         {
52                 ControlCachePolicy cachePolicy;
53                 bool initialized;
54                 AttributeCollection attributes;
55                 StateBag attrBag;
56
57                 public UserControl ()
58                 {
59                 }
60
61                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
62                 [Browsable (false)]
63                 public HttpApplicationState Application
64                 {
65                         get {
66                                 Page p = Page;
67                                 if (p == null)
68                                         return null;
69                                 return p.Application;
70                         }
71                 }
72
73                 void EnsureAttributes ()
74                 {
75                         if (attributes == null) {
76                                 attrBag = new StateBag (true);
77                                 if (IsTrackingViewState)
78                                         attrBag.TrackViewState ();
79                                 attributes = new AttributeCollection (attrBag);
80                         }
81                 }
82
83                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
84                 [Browsable (false)]
85                 public AttributeCollection Attributes
86                 {
87                         get {
88                                 EnsureAttributes ();
89                                 return attributes;
90                         }
91                 }
92
93                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94                 [Browsable (false)]
95                 public Cache Cache
96                 {
97                         get {
98                                 Page p = Page;
99                                 if (p == null)
100                                         return null;
101                                 return p.Cache;
102                         }
103                 }
104
105                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
106                 [Browsable (false)]
107                 public ControlCachePolicy CachePolicy 
108                 {
109                         get {
110                                 BasePartialCachingControl bpcc = Parent as BasePartialCachingControl;
111
112                                 if (bpcc != null)
113                                         return bpcc.CachePolicy;
114                                 
115                                 if (cachePolicy == null)
116                                         cachePolicy = new ControlCachePolicy ();
117                                 return cachePolicy;
118                         }
119                 }
120
121                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122                 [Browsable (false)]
123                 public bool IsPostBack
124                 {
125                         get {
126                                 Page p = Page;
127                                 if (p == null)
128                                         return false;
129                                 return p.IsPostBack;
130                         }
131                 }
132
133                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
134                 [Browsable (false)]
135                 public HttpRequest Request
136                 {
137                         get {
138                                 Page p = Page;
139                                 if (p == null)
140                                         return null;
141                                 return p.Request;
142                         }
143                 }
144
145                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
146                 [Browsable (false)]
147                 public HttpResponse Response
148                 {
149                         get {
150                                 Page p = Page;
151                                 if (p == null)
152                                         return null;
153                                 return p.Response;
154                         }
155                 }
156
157                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158                 [Browsable (false)]
159                 public HttpServerUtility Server
160                 {
161                         get {
162                                 Page p = Page;
163                                 if (p == null)
164                                         return null;
165                                 return p.Server;
166                         }
167                 }
168
169                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
170                 [Browsable (false)]
171                 public HttpSessionState Session
172                 {
173                         get {
174                                 Page p = Page;
175                                 if (p == null)
176                                         return null;
177                                 return p.Session;
178                         }
179                 }
180
181                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
182                 [Browsable (false)]
183                 public TraceContext Trace
184                 {
185                         get {
186                                 Page p = Page;
187                                 if (p == null)
188                                         return null;
189                                 return p.Trace;
190                         }
191                 }
192
193                 [EditorBrowsable (EditorBrowsableState.Never)]
194                 public void DesignerInitialize ()
195                 {
196                         InitRecursive (null);
197                 }
198
199                 [EditorBrowsable (EditorBrowsableState.Never)]
200                 public void InitializeAsUserControl (Page page)
201                 {
202                         if (initialized)
203                                 return;
204                         this.Page = page;
205                         InitializeAsUserControlInternal ();
206                 }
207
208                 internal void InitializeAsUserControlInternal ()
209                 {
210                         if (initialized)
211                                 return;
212                         initialized = true;
213                         WireupAutomaticEvents ();
214                         FrameworkInitialize ();
215                 }
216
217                 public string MapPath (string virtualPath)
218                 {
219                         return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
220                 }
221
222                 protected override void LoadViewState (object savedState)
223                 {
224                         if (savedState != null) {
225                                 Pair p = (Pair) savedState;
226                                 base.LoadViewState (p.First);
227                                 if (p.Second != null) {
228                                         EnsureAttributes ();
229                                         attrBag.LoadViewState (p.Second);
230                                 }
231                         }
232
233                 }
234
235                 protected internal override void OnInit (EventArgs e)
236                 {
237                         InitializeAsUserControl (Page);
238
239                         base.OnInit(e);
240                 }
241
242                 protected override object SaveViewState ()
243                 {
244                         object baseState = base.SaveViewState();
245                         object attrState = null;
246                         if (attributes != null)
247                                 attrState = attrBag.SaveViewState ();
248                         if (baseState == null && attrState == null)
249                                 return null;
250                         return new Pair (baseState, attrState);
251                 }
252
253                 string IAttributeAccessor.GetAttribute (string name)
254                 {
255                         if (attributes == null)
256                                 return null;
257                         return attributes [name];
258                 }
259                 
260                 void IAttributeAccessor.SetAttribute (string name, string value)
261                 {
262                         EnsureAttributes ();
263                         Attributes [name] = value;
264                 }
265
266                 string IUserControlDesignerAccessor.InnerText
267                 {
268                         get {
269                                 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
270                                 if (innerText == null)
271                                         return string.Empty; 
272                                 return innerText;
273                         }
274                         set { ViewState["!DesignTimeInnerText"] = value; }
275                 }
276
277                 string IUserControlDesignerAccessor.TagName
278                 {
279                         get {
280                                 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
281                                 if (innerTag == null)
282                                         return string.Empty; 
283                                 return innerTag;
284                         }
285                         set { ViewState["!DesignTimeTagName"] = value; }
286                 }
287
288                 [MonoTODO ("Not implemented")]
289                 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
290                 {
291                         throw new NotImplementedException ();
292                 }
293
294                 [MonoTODO ("Not implemented")]
295                 bool IFilterResolutionService.EvaluateFilter (string filterName)
296                 {
297                         throw new NotImplementedException ();
298                 }
299         }
300 }