d801a1ba49f3e08b8d4c18a07d188d609f1c81c7
[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-2010 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         [ToolboxData("<{0}:Label runat=\"server\" Text=\"Label\"></{0}:Label>")]
47         [ControlValueProperty ("Text", null)]
48         public class Label : WebControl, ITextControl
49         {
50                 [Bindable(true)]
51                 [DefaultValue("")]
52                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
53                 [Localizable (true)]
54                 [WebSysDescription ("")]
55                 [WebCategory ("Appearance")]
56                 public virtual string Text {
57                         get { return ViewState.GetString ("Text", String.Empty); }
58                         set {
59                                 ViewState ["Text"] = value;
60                                 if (HasControls ())
61                                         Controls.Clear ();
62                         }
63                 }
64
65                 [IDReferenceProperty (typeof (Control))]
66                 [TypeConverter (typeof (AssociatedControlConverter))]
67                 [Themeable (false)]
68                 [DefaultValue("")]
69                 [WebSysDescription ("")]
70                 [WebCategory ("Accessibility")]
71                 public virtual string AssociatedControlID {
72                         get { return ViewState.GetString ("AssociatedControlID", String.Empty); }
73                         set { ViewState ["AssociatedControlID"] = value; }
74                 }
75 #if NET_4_0
76                 public override bool SupportsDisabledAttribute {
77                         get { return RenderingCompatibilityLessThan40; }
78                 }
79 #endif
80                 protected override void LoadViewState (object savedState)
81                 {
82                         base.LoadViewState (savedState);
83
84                         // Make sure we clear child controls when this happens
85                         if (ViewState ["Text"] != null)
86                                 Text = (string) ViewState ["Text"];
87                 }
88
89                 protected override void AddParsedSubObject (object obj)
90                 {
91                         if (HasControls ()) {
92                                 base.AddParsedSubObject (obj);
93                                 return;
94                         }
95                         
96                         LiteralControl lc = obj as LiteralControl;
97
98                         if (lc == null) {
99                                 string s = Text;
100                                 if (s.Length != 0) {
101                                         Text = null;
102                                         Controls.Add (new LiteralControl (s));
103                                 }
104                                 base.AddParsedSubObject (obj);
105                         } else {
106                                 Text = lc.Text;
107                         }
108                 }
109                 
110                 protected internal override void RenderContents (HtmlTextWriter writer)
111                 {
112                         if (HasControls () || HasRenderMethodDelegate ())
113                                 base.RenderContents (writer);
114                         else
115                                 writer.Write (Text);
116                 }
117
118                 protected override HtmlTextWriterTag TagKey {
119                         get { return String.IsNullOrEmpty (AssociatedControlID) ? HtmlTextWriterTag.Span : HtmlTextWriterTag.Label; }
120                 }
121
122                 protected override void AddAttributesToRender (HtmlTextWriter writer)
123                 {
124                         base.AddAttributesToRender (writer);
125                         if (!String.IsNullOrEmpty (AssociatedControlID))
126                                 writer.AddAttribute (HtmlTextWriterAttribute.For, NamingContainer.FindControl (AssociatedControlID).ClientID);
127                 }
128         }
129 }