2006-11-13 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlContainerControl.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlContainerControl.cs
3 //
4 // Authors:
5 //      Bob Smith <bob@thestuff.net>
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Bob Smith
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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
32 using System.ComponentModel;
33 using System.Security.Permissions;
34 using System.Text;
35
36 //LAMESPEC: The dox talk about HttpException but are very ambigious.
37 //TODO: Check to see if Render really is overridden instead of a LiteralControl being added. It apears that this is the
38 //case due to testing. Anything inside the block is overwritten by the content of this control, so it doesnt apear
39 //to do anything with children.
40
41 //TODO: If Test.InnerText = Test.InnerHtml without ever assigning anything into InnerHtml, you get this:
42 // Exception Details: System.Web.HttpException: Cannot get inner content of Message because the contents are not literal.
43 //[HttpException (0x80004005): Cannot get inner content of Message because the contents are not literal.]
44 //  System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml() +278
45 //  ASP.test3_aspx.AnchorBtn_Click(Object Source, EventArgs E) in \\genfs2\www24\bobsmith11\test3.aspx:6
46 //  System.Web.UI.HtmlControls.HtmlAnchor.OnServerClick(EventArgs e) +108
47 //  System.Web.UI.HtmlControls.HtmlAnchor.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +26
48 //  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
49 //  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +149
50 //  System.Web.UI.Page.ProcessRequestMain() +660
51
52
53 namespace System.Web.UI.HtmlControls
54 {
55         // CAS
56         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
57         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
58         public abstract class HtmlContainerControl : HtmlControl {
59
60 #if NET_2_0
61                 protected
62 #else
63                 public
64 #endif
65                 HtmlContainerControl () : this ("span") {}
66                 
67                 public HtmlContainerControl (string tag) : base(tag) {}
68
69                 [HtmlControlPersistable (false)]
70                 [BrowsableAttribute(false)]
71                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
72                 public virtual string InnerHtml
73                 {
74                         get {
75                                 if (Controls.Count == 0)
76                                         return String.Empty;
77                                 
78                                 if (Controls.Count == 1) {
79                                         Control ctrl = Controls [0];
80                                         LiteralControl lc = ctrl as LiteralControl;
81                                         if (lc != null)
82                                                 return lc.Text;
83
84                                         DataBoundLiteralControl dblc = ctrl as DataBoundLiteralControl;
85                                         if (dblc != null)
86                                                 return dblc.Text;
87                                 }
88                                 
89                                 throw new HttpException ("There is no literal content!");
90                         }
91
92                         set {
93                                 Controls.Clear ();
94                                 Controls.Add (new LiteralControl (value));
95                                 if (value == null)
96                                         ViewState.Remove ("innerhtml");
97                                 else
98                                         ViewState ["innerhtml"] = value;
99                         }
100                 }
101
102                 [HtmlControlPersistable (false)]
103                 [BrowsableAttribute(false)]
104                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
105                 public virtual string InnerText
106                 {
107                         get {
108                                 return HttpUtility.HtmlDecode (InnerHtml);
109                         }
110
111                         set {
112                                 InnerHtml = HttpUtility.HtmlEncode (value);
113                         }
114                 }
115                 
116 #if NET_2_0
117                 protected internal
118 #else
119                 protected
120 #endif          
121                 override void Render (HtmlTextWriter writer)
122                 {
123                         RenderBeginTag (writer);
124                         RenderChildren (writer);
125                         RenderEndTag (writer);
126                 }
127
128                 protected virtual void RenderEndTag (HtmlTextWriter writer)
129                 {
130                         writer.WriteEndTag (TagName);
131                 }
132
133                 protected override void RenderAttributes (HtmlTextWriter writer)
134                 {
135                         ViewState.Remove ("innerhtml");
136                         base.RenderAttributes (writer);
137                 }
138
139                 /* we need to override this because our base class
140                  * (HtmlControl) returns an instance of
141                  * EmptyControlCollection. */
142                 protected override ControlCollection CreateControlCollection ()
143                 {
144                         return new ControlCollection (this);
145                 }
146
147                 protected override void LoadViewState (object savedState)
148                 {
149                         if (savedState != null) {
150                                 base.LoadViewState (savedState);
151                                 string inner = ViewState ["innerhtml"] as string;
152                                 if (inner != null)
153                                         InnerHtml = inner;
154                         }
155                 }
156         }
157 }