Label.cs 4.2 KB

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