HtmlControl.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 RenderBeginTag (HtmlTextWriter writer)
  56. {
  57. writer.WriteBeginTag (TagName);
  58. RenderAttributes (writer);
  59. writer.Write ('>');
  60. }
  61. protected override void Render (HtmlTextWriter writer)
  62. {
  63. RenderBeginTag (writer);
  64. }
  65. protected virtual void RenderAttributes(HtmlTextWriter writer){
  66. if (ID != null){
  67. writer.WriteAttribute("id",ClientID);
  68. }
  69. Attributes.Render(writer);
  70. }
  71. public AttributeCollection Attributes
  72. {
  73. get { return _attributes; }
  74. }
  75. public bool Disabled
  76. {
  77. get { return _disabled; }
  78. set { _disabled = value; }
  79. }
  80. public CssStyleCollection Style
  81. {
  82. get { return _attributes.CssStyle; }
  83. }
  84. public virtual string TagName
  85. {
  86. get { return _tagName; }
  87. }
  88. }
  89. }