HtmlControl.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Globalization;
  12. using System.Web;
  13. using System.Web.UI;
  14. namespace System.Web.UI.HtmlControls{
  15. public abstract class HtmlControl : Control, IAttributeAccessor
  16. {
  17. private string _tagName = "span";
  18. //TODO: Is this correct, or is the StateBag really the ViewState?
  19. private AttributeCollection _attributes = new AttributeCollection(new StateBag(true));
  20. private bool _disabled = false;
  21. public HtmlControl(){}
  22. public HtmlControl(string tag)
  23. {
  24. if(tag != null && tag != String.Empty) _tagName = tag;
  25. }
  26. internal static string AttributeToString(int n){
  27. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  28. return null;
  29. }
  30. internal static string AttributeToString(string s){
  31. if (s != null && s.Length != 0) return s;
  32. return null;
  33. }
  34. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  35. string attr = Attributes[attribName];
  36. if (attr != null){
  37. if (attr.Length != 0){
  38. try{
  39. attr = ResolveUrl(attr);
  40. }
  41. catch (Exception e) {
  42. throw new HttpException(attribName + " property had malformed url");
  43. }
  44. writer.WriteAttribute(attribName, attr);
  45. Attributes.Remove(attribName);
  46. }
  47. }
  48. }
  49. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  50. return Attributes[name];
  51. }
  52. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  53. Attributes[name] = value;
  54. }
  55. protected virtual void RenderAttributes(HtmlTextWriter writer){
  56. if (ID != null){
  57. writer.WriteAttribute("id",ClientID);
  58. }
  59. Attributes.Render(writer);
  60. }
  61. internal void WriteOnClickAttribute(HtmlTextWriter writer, bool submitsAutomatically, bool submitsProgramatically, bool causesValidation) {
  62. string local1;
  63. string local2;
  64. string local3;
  65. AttributeCollection attr = Attributes;
  66. local1 = null;
  67. if (submitsAutomatically) {
  68. if ((causesValidation))
  69. local1 = System.Web.UI.Utils.GetClientValidatedEvent(Page);
  70. }
  71. else if (submitsProgramatically) {
  72. if (causesValidation)
  73. local1 = System.Web.UI.Utils.GetClientValidatedPostBack(this);
  74. else
  75. local1 = Page.GetPostBackClientEvent(this, String.Empty);
  76. }
  77. if (local1 != null) {
  78. local2 = attr["language"];
  79. if (local2 != null)
  80. attr.Remove("language");
  81. writer.WriteAttribute("language", "javascript");
  82. local3 = attr["onclick"];
  83. if (local3 != null) {
  84. attr.Remove("onclick");
  85. writer.WriteAttribute("onclick", local3 + " " + local1);
  86. return;
  87. }
  88. writer.WriteAttribute("onclick", local1);
  89. }
  90. }
  91. public AttributeCollection Attributes
  92. {
  93. get
  94. {
  95. return _attributes;
  96. }
  97. }
  98. public bool Disabled
  99. {
  100. get
  101. {
  102. return _disabled;
  103. }
  104. set
  105. {
  106. _disabled = value;
  107. }
  108. }
  109. public CssStyleCollection Style
  110. {
  111. get
  112. {
  113. return _attributes.CssStyle;
  114. }
  115. }
  116. public virtual string TagName
  117. {
  118. get
  119. {
  120. return _tagName;
  121. }
  122. }
  123. }
  124. }