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