Label.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Web.UI.WebControls.Label.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  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. using System.ComponentModel;
  32. namespace System.Web.UI.WebControls {
  33. [ControlBuilder(typeof(LabelControlBuilder))]
  34. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  35. [DefaultProperty("Text")]
  36. [Designer("System.Web.UI.Design.WebControls.LabelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. #if NET_2_0
  38. [ToolboxData("<{0}:Label runat=\"server\" Text=\"Label\"></{0}:Label>")]
  39. [ParseChildren(false, ChildControlType = typeof(Control))]
  40. [ControlValueProperty ("Text", null)]
  41. #else
  42. [ToolboxData("<{0}:Label runat=server>Label</{0}:Label>")]
  43. [ParseChildren(false)]
  44. #endif
  45. public class Label : WebControl
  46. #if NET_2_0
  47. , ITextControl
  48. #endif
  49. {
  50. [Bindable(true)]
  51. [DefaultValue("")]
  52. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  53. #if NET_2_0
  54. [Localizable (true)]
  55. #endif
  56. [WebSysDescription ("")]
  57. [WebCategory ("Appearance")]
  58. public virtual string Text {
  59. get {
  60. return ViewState.GetString ("Text", "");
  61. }
  62. set {
  63. ViewState ["Text"] = value;
  64. if (HasControls ())
  65. Controls.Clear ();
  66. }
  67. }
  68. // Added in .net 1.1 sp1
  69. #if NET_2_0
  70. [IDReferenceProperty (typeof (Control))]
  71. [TypeConverter (typeof (AssociatedControlConverter))]
  72. [Themeable (false)]
  73. #endif
  74. [DefaultValue("")]
  75. [WebSysDescription ("")]
  76. [WebCategory ("Accessibility")]
  77. public virtual string AssociatedControlID {
  78. get {
  79. return ViewState.GetString ("AssociatedControlID", "");
  80. }
  81. set {
  82. ViewState ["AssociatedControlID"] = value;
  83. }
  84. }
  85. protected override void LoadViewState (object savedState)
  86. {
  87. base.LoadViewState (savedState);
  88. // Make sure we clear child controls when this happens
  89. if (ViewState ["Text"] != null)
  90. Text = (string) ViewState ["Text"];
  91. }
  92. protected override void AddParsedSubObject (object obj)
  93. {
  94. if (HasControls ()) {
  95. base.AddParsedSubObject (obj);
  96. return;
  97. }
  98. LiteralControl lc = obj as LiteralControl;
  99. if (lc == null) {
  100. string s = Text;
  101. if (s.Length != 0) {
  102. Text = null;
  103. Controls.Add (new LiteralControl (s));
  104. }
  105. base.AddParsedSubObject (obj);
  106. } else {
  107. Text = lc.Text;
  108. }
  109. }
  110. #if NET_2_0
  111. protected internal
  112. #else
  113. protected
  114. #endif
  115. override void RenderContents (HtmlTextWriter writer)
  116. {
  117. if (HasControls ())
  118. base.RenderContents (writer);
  119. else
  120. writer.Write (Text);
  121. }
  122. // Added in .net 1.1 sp1
  123. protected override HtmlTextWriterTag TagKey {
  124. get {
  125. return AssociatedControlID == "" ? HtmlTextWriterTag.Span : HtmlTextWriterTag.Label;
  126. }
  127. }
  128. // Added in .net 1.1 sp1
  129. protected override void AddAttributesToRender (HtmlTextWriter writer)
  130. {
  131. base.AddAttributesToRender (writer);
  132. if (AssociatedControlID != "")
  133. writer.AddAttribute (HtmlTextWriterAttribute.For, NamingContainer.FindControl (AssociatedControlID).ClientID);
  134. }
  135. }
  136. }