2
0

HtmlControl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // System.Web.UI.HtmlControls.HtmlControl.cs
  3. //
  4. // Author
  5. // Bob Smith <[email protected]>
  6. //
  7. //
  8. // (C) Bob Smith
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.ComponentModel.Design;
  13. using System.Globalization;
  14. using System.Web;
  15. using System.Web.UI;
  16. namespace System.Web.UI.HtmlControls{
  17. [ToolboxItem(false)]
  18. [Designer ("System.Web.UI.Design.HtmlIntrinsicControlDesigner, " + Consts.AssemblySystem_Design,
  19. "System.ComponentModel.Design.IDesigner")]
  20. public abstract class HtmlControl : Control, IAttributeAccessor
  21. {
  22. internal string _tagName;
  23. private AttributeCollection _attributes;
  24. #if NET_2_0
  25. protected
  26. #else
  27. public
  28. #endif
  29. HtmlControl() : this ("span") {}
  30. #if NET_2_0
  31. protected
  32. #else
  33. public
  34. #endif
  35. HtmlControl(string tag)
  36. {
  37. _tagName = tag;
  38. }
  39. protected override ControlCollection CreateControlCollection ()
  40. {
  41. return new EmptyControlCollection (this);
  42. }
  43. internal static string AttributeToString(int n){
  44. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  45. return null;
  46. }
  47. internal static string AttributeToString(string s){
  48. if (s != null && s.Length != 0) return s;
  49. return null;
  50. }
  51. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  52. string attr = Attributes[attribName];
  53. if (attr != null){
  54. if (attr.Length != 0){
  55. try{
  56. attr = ResolveUrl(attr);
  57. }
  58. catch (Exception) {
  59. throw new HttpException(attribName + " property had malformed url");
  60. }
  61. writer.WriteAttribute(attribName, attr);
  62. Attributes.Remove(attribName);
  63. }
  64. }
  65. }
  66. #if NET_2_0
  67. /* keep these two methods in sync with the
  68. * IAttributeAccessor iface methods below */
  69. protected virtual string GetAttribute (string name)
  70. {
  71. return Attributes[name];
  72. }
  73. protected virtual void SetAttribute (string name, string value)
  74. {
  75. Attributes[name] = value;
  76. }
  77. #endif
  78. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  79. return Attributes[name];
  80. }
  81. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  82. Attributes[name] = value;
  83. }
  84. protected virtual void RenderBeginTag (HtmlTextWriter writer)
  85. {
  86. writer.WriteBeginTag (TagName);
  87. RenderAttributes (writer);
  88. writer.Write ('>');
  89. }
  90. #if NET_2_0
  91. protected internal
  92. #else
  93. protected
  94. #endif
  95. override void Render (HtmlTextWriter writer)
  96. {
  97. RenderBeginTag (writer);
  98. }
  99. protected virtual void RenderAttributes(HtmlTextWriter writer){
  100. if (ID != null){
  101. writer.WriteAttribute("id",ClientID);
  102. }
  103. Attributes.Render(writer);
  104. }
  105. [Browsable(false)]
  106. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  107. public AttributeCollection Attributes
  108. {
  109. get {
  110. if (_attributes == null)
  111. _attributes = new AttributeCollection (ViewState);
  112. return _attributes;
  113. }
  114. }
  115. [DefaultValue(false)]
  116. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  117. [WebCategory("Behavior")]
  118. #if NET_2_0
  119. /* Can't find this type in the docs */
  120. //[TypeConverter (typeof(System.Web.UI.MinimizableAttributeTypeConverter))]
  121. #endif
  122. public bool Disabled
  123. {
  124. get {
  125. string disableAttr = Attributes["disabled"] as string;
  126. return (disableAttr != null);
  127. }
  128. set {
  129. if (!value)
  130. Attributes.Remove ("disabled");
  131. else
  132. Attributes["disabled"] = "disabled";
  133. }
  134. }
  135. [Browsable(false)]
  136. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  137. public CssStyleCollection Style
  138. {
  139. get { return Attributes.CssStyle; }
  140. }
  141. [DefaultValue("")]
  142. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  143. [WebCategory("Appearance")]
  144. public virtual string TagName
  145. {
  146. get { return _tagName; }
  147. }
  148. protected override bool ViewStateIgnoresCase
  149. {
  150. get {
  151. return true;
  152. }
  153. }
  154. }
  155. }