Commit of changes to core files, on behalf of the team
[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 //
11
12 using System;
13 using System.ComponentModel;
14 using System.Text;
15 using System.Web;
16 using System.Web.UI;
17
18 //LAMESPEC: The dox talk about HttpException but are very ambigious.
19 //TODO: Check to see if Render really is overridden instead of a LiteralControl being added. It apears that this is the
20 //case due to testing. Anything inside the block is overwritten by the content of this control, so it doesnt apear
21 //to do anything with children.
22 // a doc references this. add? protected override ControlCollection CreateControlCollection();
23
24 //TODO: If Test.InnerText = Test.InnerHtml without ever assigning anything into InnerHtml, you get this:
25 // Exception Details: System.Web.HttpException: Cannot get inner content of Message because the contents are not literal.
26 //[HttpException (0x80004005): Cannot get inner content of Message because the contents are not literal.]
27 //  System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml() +278
28 //  ASP.test3_aspx.AnchorBtn_Click(Object Source, EventArgs E) in \\genfs2\www24\bobsmith11\test3.aspx:6
29 //  System.Web.UI.HtmlControls.HtmlAnchor.OnServerClick(EventArgs e) +108
30 //  System.Web.UI.HtmlControls.HtmlAnchor.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +26
31 //  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
32 //  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +149
33 //  System.Web.UI.Page.ProcessRequestMain() +660
34
35
36 namespace System.Web.UI.HtmlControls
37 {
38         public abstract class HtmlContainerControl : HtmlControl{
39                 
40 #if NET_2_0
41                 protected
42 #else
43                 public
44 #endif
45                 HtmlContainerControl () : this ("span") {}
46                 
47                 public HtmlContainerControl (string tag) : base(tag) {}
48
49                 [HtmlControlPersistable (false)]
50                 [BrowsableAttribute(false)]
51                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
52                 public virtual string InnerHtml
53                 {
54                         get {
55                                 if (Controls.Count == 0)
56                                         return String.Empty;
57
58                                 bool is_literal = true;
59                                 StringBuilder text = new StringBuilder ();
60                                 foreach (Control ctrl in Controls) {
61                                         LiteralControl lc = ctrl as LiteralControl;
62                                         if (lc == null) {
63                                                 is_literal = false;
64                                                 break;
65                                         }
66                                         text.Append (lc.Text);
67                                 }
68                                         
69                                 if (!is_literal)
70                                         throw new HttpException ("There is no literal content!");
71
72                                 return text.ToString ();
73                         }
74
75                         set {
76                                 Controls.Clear ();
77                                 Controls.Add (new LiteralControl (value));
78                                 if (value == null)
79                                         ViewState.Remove ("innerhtml");
80                                 else
81                                         ViewState ["innerhtml"] = value;
82                         }
83                 }
84
85                 [HtmlControlPersistable (false)]
86                 [BrowsableAttribute(false)]
87                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88                 public virtual string InnerText
89                 {
90                         get {
91                                 return HttpUtility.HtmlDecode (InnerHtml);
92                         }
93
94                         set {
95                                 InnerHtml = HttpUtility.HtmlEncode (value);
96                         }
97                 }
98                 
99 #if NET_2_0
100                 protected internal
101 #else
102                 protected
103 #endif          
104                 override void Render (HtmlTextWriter writer)
105                 {
106                         RenderBeginTag (writer);
107                         RenderChildren (writer);
108                         RenderEndTag (writer);
109                 }
110
111                 protected virtual void RenderEndTag (HtmlTextWriter writer)
112                 {
113                         writer.WriteEndTag (TagName);
114                 }
115
116                 protected override void RenderAttributes (HtmlTextWriter writer)
117                 {
118                         ViewState.Remove ("innerhtml");
119                         base.RenderAttributes (writer);
120                 }
121
122                 protected override ControlCollection CreateControlCollection ()
123                 {
124                         return new ControlCollection (this);
125                 }
126
127                 protected override void LoadViewState (object savedState)
128                 {
129                         if (savedState != null) {
130                                 base.LoadViewState (savedState);
131                                 string inner = ViewState ["innerhtml"] as string;
132                                 if (inner != null)
133                                         InnerHtml = inner;
134                         }
135                 }
136         }
137 }