* UI/ParseChildrenAttribute.cs: fixed ChildControlType property to compliant to .net
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Label.cs
1 //
2 // System.Web.UI.WebControls.Label.cs
3 //
4 // Authors:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // TODO: Are we missing something in LoadViewState?
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
32 using System.ComponentModel;
33 using System.Security.Permissions;
34
35 namespace System.Web.UI.WebControls {
36
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         // attributes
41         [ControlBuilder(typeof(LabelControlBuilder))]
42         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
43         [DefaultProperty("Text")]
44         [Designer("System.Web.UI.Design.WebControls.LabelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         [ParseChildren (false)]
46 #if NET_2_0
47         [ToolboxData("<{0}:Label runat=\"server\" Text=\"Label\"></{0}:Label>")]
48         [ControlValueProperty ("Text", null)]
49 #else   
50         [ToolboxData("<{0}:Label runat=server>Label</{0}:Label>")]
51 #endif          
52         public class Label : WebControl
53 #if NET_2_0
54         , ITextControl
55 #endif          
56         {
57                 [Bindable(true)]
58                 [DefaultValue("")]
59                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
60 #if NET_2_0
61                 [Localizable (true)]
62 #endif          
63                 [WebSysDescription ("")]
64                 [WebCategory ("Appearance")]
65                 public virtual string Text {
66                         get {
67                                 return ViewState.GetString ("Text", "");
68                         }
69                         set {
70                                 ViewState ["Text"] = value;
71                                 if (HasControls ())
72                                         Controls.Clear ();
73                         }
74                 }
75
76                 // Added in .net 1.1 sp1
77 #if NET_2_0
78                 [IDReferenceProperty (typeof (Control))]
79                 [TypeConverter (typeof (AssociatedControlConverter))]
80                 [Themeable (false)]
81 #endif          
82                 [DefaultValue("")]
83                 [WebSysDescription ("")]
84                 [WebCategory ("Accessibility")]
85                 public virtual string AssociatedControlID {
86                         get {
87                                 return ViewState.GetString ("AssociatedControlID", "");
88                         }
89                         set {
90                                 ViewState ["AssociatedControlID"] = value;
91                         }
92                 }
93
94                 protected override void LoadViewState (object savedState)
95                 {
96                         base.LoadViewState (savedState);
97
98                         // Make sure we clear child controls when this happens
99                         if (ViewState ["Text"] != null)
100                                 Text = (string) ViewState ["Text"];
101                 }
102
103                 protected override void AddParsedSubObject (object obj)
104                 {
105                         if (HasControls ()) {
106                                 base.AddParsedSubObject (obj);
107                                 return;
108                         }
109                         
110                         LiteralControl lc = obj as LiteralControl;
111
112                         if (lc == null) {
113                                 string s = Text;
114                                 if (s.Length != 0) {
115                                         Text = null;
116                                         Controls.Add (new LiteralControl (s));
117                                 }
118                                 base.AddParsedSubObject (obj);
119                         } else {
120                                 Text = lc.Text;
121                         }
122                 }
123
124 #if NET_2_0
125                 protected internal 
126 #else
127                 protected
128 #endif
129                 override void OnPreRender (EventArgs e)
130                 {
131                         base.OnPreRender (e);
132                         ControlStyle.AlwaysRenderTextDecoration = true;
133                 }
134                 
135 #if NET_2_0
136                 protected internal
137 #else           
138                 protected
139 #endif          
140                 override void RenderContents (HtmlTextWriter writer)
141                 {
142                         if (HasControls ())
143                                 base.RenderContents (writer);
144                         else
145                                 writer.Write (Text);
146                 }
147
148                 // Added in .net 1.1 sp1
149                 protected override HtmlTextWriterTag TagKey {
150                         get {
151                                 return AssociatedControlID == "" ? HtmlTextWriterTag.Span : HtmlTextWriterTag.Label;
152                         }
153                 }
154
155                 // Added in .net 1.1 sp1
156                 protected override void AddAttributesToRender (HtmlTextWriter writer)
157                 {
158                         base.AddAttributesToRender (writer);
159                         if (AssociatedControlID != "")
160                                 writer.AddAttribute (HtmlTextWriterAttribute.For, NamingContainer.FindControl (AssociatedControlID).ClientID);
161                 }
162         }
163 }