HtmlControl.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. protected override ControlCollection CreateControlCollection ()
  27. {
  28. return new EmptyControlCollection (this);
  29. }
  30. internal static string AttributeToString(int n){
  31. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  32. return null;
  33. }
  34. internal static string AttributeToString(string s){
  35. if (s != null && s.Length != 0) return s;
  36. return null;
  37. }
  38. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  39. string attr = Attributes[attribName];
  40. if (attr != null){
  41. if (attr.Length != 0){
  42. try{
  43. attr = ResolveUrl(attr);
  44. }
  45. catch (Exception e) {
  46. throw new HttpException(attribName + " property had malformed url");
  47. }
  48. writer.WriteAttribute(attribName, attr);
  49. Attributes.Remove(attribName);
  50. }
  51. }
  52. }
  53. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  54. return Attributes[name];
  55. }
  56. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  57. Attributes[name] = value;
  58. }
  59. protected virtual void RenderBeginTag (HtmlTextWriter writer)
  60. {
  61. writer.WriteBeginTag (TagName);
  62. RenderAttributes (writer);
  63. writer.Write ('>');
  64. }
  65. protected override void Render (HtmlTextWriter writer)
  66. {
  67. RenderBeginTag (writer);
  68. }
  69. protected virtual void RenderAttributes(HtmlTextWriter writer){
  70. if (ID != null){
  71. writer.WriteAttribute("id",ClientID);
  72. }
  73. Attributes.Render(writer);
  74. }
  75. public AttributeCollection Attributes
  76. {
  77. get { return _attributes; }
  78. }
  79. public bool Disabled
  80. {
  81. get { return _disabled; }
  82. set { _disabled = value; }
  83. }
  84. public CssStyleCollection Style
  85. {
  86. get { return _attributes.CssStyle; }
  87. }
  88. public virtual string TagName
  89. {
  90. get { return _tagName; }
  91. }
  92. }
  93. }