This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 public HtmlContainerControl () : this ("span") {}
41                 
42                 public HtmlContainerControl (string tag) : base(tag) {}
43
44                 [HtmlControlPersistable (false)]
45                 [BrowsableAttribute(false)]
46                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
47                 public virtual string InnerHtml
48                 {
49                         get {
50                                 if (Controls.Count == 0)
51                                         return String.Empty;
52
53                                 bool is_literal = true;
54                                 StringBuilder text = new StringBuilder ();
55                                 foreach (Control ctrl in Controls) {
56                                         LiteralControl lc = ctrl as LiteralControl;
57                                         if (lc == null) {
58                                                 is_literal = false;
59                                                 break;
60                                         }
61                                         text.Append (lc.Text);
62                                 }
63                                         
64                                 if (!is_literal)
65                                         throw new HttpException ("There is no literal content!");
66
67                                 return text.ToString ();
68                         }
69
70                         set {
71                                 Controls.Clear ();
72                                 Controls.Add (new LiteralControl (value));
73                                 ViewState ["innerhtml"] = value;
74                         }
75                 }
76
77                 [HtmlControlPersistable (false)]
78                 [BrowsableAttribute(false)]
79                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80                 public virtual string InnerText
81                 {
82                         get {
83                                 return HttpUtility.HtmlDecode (InnerHtml);
84                         }
85
86                         set {
87                                 InnerHtml = HttpUtility.HtmlEncode (value);
88                         }
89                 }
90                 
91                 protected override void Render (HtmlTextWriter writer)
92                 {
93                         RenderBeginTag (writer);
94                         RenderChildren (writer);
95                         RenderEndTag (writer);
96                 }
97
98                 protected virtual void RenderEndTag (HtmlTextWriter writer)
99                 {
100                         writer.WriteEndTag (TagName);
101                 }
102
103                 protected override void RenderAttributes (HtmlTextWriter writer)
104                 {
105                         ViewState.Remove ("innerhtml");
106                         base.RenderAttributes (writer);
107                 }
108
109                 protected override ControlCollection CreateControlCollection ()
110                 {
111                         return new ControlCollection (this);
112                 }
113
114                 protected override void LoadViewState (object savedState)
115                 {
116                         if (savedState != null) {
117                                 base.LoadViewState (savedState);
118                                 string inner = ViewState ["innerhtml"] as string;
119                                 if (inner != null)
120                                         InnerHtml = inner;
121                         }
122                 }
123         }
124 }