HtmlControl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public HtmlControl() : this ("span") {}
  25. public HtmlControl(string tag)
  26. {
  27. _tagName = tag;
  28. }
  29. protected override ControlCollection CreateControlCollection ()
  30. {
  31. return new EmptyControlCollection (this);
  32. }
  33. internal static string AttributeToString(int n){
  34. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  35. return null;
  36. }
  37. internal static string AttributeToString(string s){
  38. if (s != null && s.Length != 0) return s;
  39. return null;
  40. }
  41. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  42. string attr = Attributes[attribName];
  43. if (attr != null){
  44. if (attr.Length != 0){
  45. try{
  46. attr = ResolveUrl(attr);
  47. }
  48. catch (Exception) {
  49. throw new HttpException(attribName + " property had malformed url");
  50. }
  51. writer.WriteAttribute(attribName, attr);
  52. Attributes.Remove(attribName);
  53. }
  54. }
  55. }
  56. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  57. return Attributes[name];
  58. }
  59. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  60. Attributes[name] = value;
  61. }
  62. protected virtual void RenderBeginTag (HtmlTextWriter writer)
  63. {
  64. writer.WriteBeginTag (TagName);
  65. RenderAttributes (writer);
  66. writer.Write ('>');
  67. }
  68. protected override void Render (HtmlTextWriter writer)
  69. {
  70. RenderBeginTag (writer);
  71. }
  72. protected virtual void RenderAttributes(HtmlTextWriter writer){
  73. if (ID != null){
  74. writer.WriteAttribute("id",ClientID);
  75. }
  76. Attributes.Render(writer);
  77. }
  78. [Browsable(false)]
  79. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  80. public AttributeCollection Attributes
  81. {
  82. get {
  83. if (_attributes == null)
  84. _attributes = new AttributeCollection (ViewState);
  85. return _attributes;
  86. }
  87. }
  88. [DefaultValue(false)]
  89. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  90. [WebCategory("Behavior")]
  91. public bool Disabled
  92. {
  93. get {
  94. string disableAttr = Attributes["disabled"] as string;
  95. return (disableAttr != null);
  96. }
  97. set {
  98. if (!value)
  99. Attributes.Remove ("disabled");
  100. else
  101. Attributes["disabled"] = "disabled";
  102. }
  103. }
  104. [Browsable(false)]
  105. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  106. public CssStyleCollection Style
  107. {
  108. get { return Attributes.CssStyle; }
  109. }
  110. [DefaultValue("")]
  111. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  112. [WebCategory("Appearance")]
  113. public virtual string TagName
  114. {
  115. get { return _tagName; }
  116. }
  117. protected override bool ViewStateIgnoresCase
  118. {
  119. get {
  120. return true;
  121. }
  122. }
  123. }
  124. }