HtmlControl.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // System.Web.UI.HtmlControls.HtmlControl.cs
  3. //
  4. // Author
  5. // Bob Smith <[email protected]>
  6. //
  7. //
  8. // (C) Bob Smith
  9. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.Globalization;
  33. using System.Security.Permissions;
  34. namespace System.Web.UI.HtmlControls{
  35. // CAS
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [ToolboxItem(false)]
  40. [Designer ("System.Web.UI.Design.HtmlIntrinsicControlDesigner, " + Consts.AssemblySystem_Design,
  41. "System.ComponentModel.Design.IDesigner")]
  42. public abstract class HtmlControl : Control, IAttributeAccessor
  43. {
  44. internal string _tagName;
  45. AttributeCollection _attributes;
  46. protected HtmlControl() : this ("span") {}
  47. protected HtmlControl(string tag)
  48. {
  49. _tagName = tag;
  50. }
  51. protected override ControlCollection CreateControlCollection ()
  52. {
  53. return new EmptyControlCollection (this);
  54. }
  55. internal static string AttributeToString(int n){
  56. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  57. return null;
  58. }
  59. internal static string AttributeToString(string s){
  60. if (s != null && s.Length != 0) return s;
  61. return null;
  62. }
  63. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  64. string attr = Attributes[attribName];
  65. if (attr != null){
  66. if (attr.Length != 0){
  67. try{
  68. attr = ResolveClientUrl(attr);
  69. }
  70. catch (Exception) {
  71. throw new HttpException(attribName + " property had malformed url");
  72. }
  73. writer.WriteAttribute(attribName, attr);
  74. Attributes.Remove(attribName);
  75. }
  76. }
  77. }
  78. /* keep these two methods in sync with the
  79. * IAttributeAccessor iface methods below */
  80. protected virtual string GetAttribute (string name)
  81. {
  82. return Attributes[name];
  83. }
  84. protected virtual void SetAttribute (string name, string value)
  85. {
  86. Attributes[name] = value;
  87. }
  88. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  89. return Attributes[name];
  90. }
  91. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  92. Attributes[name] = value;
  93. }
  94. protected virtual void RenderBeginTag (HtmlTextWriter writer)
  95. {
  96. writer.WriteBeginTag (TagName);
  97. RenderAttributes (writer);
  98. writer.Write ('>');
  99. }
  100. protected internal override void Render (HtmlTextWriter writer)
  101. {
  102. RenderBeginTag (writer);
  103. }
  104. protected virtual void RenderAttributes(HtmlTextWriter writer){
  105. if (ID != null){
  106. writer.WriteAttribute("id",ClientID);
  107. }
  108. Attributes.Render(writer);
  109. }
  110. [Browsable(false)]
  111. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  112. public AttributeCollection Attributes {
  113. get {
  114. if (_attributes == null)
  115. _attributes = new AttributeCollection (ViewState);
  116. return _attributes;
  117. }
  118. }
  119. [DefaultValue(false)]
  120. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  121. [WebSysDescription("")]
  122. [WebCategory("Behavior")]
  123. [TypeConverter (typeof(MinimizableAttributeTypeConverter))]
  124. public bool Disabled {
  125. get {
  126. string disableAttr = Attributes["disabled"] as string;
  127. return (disableAttr != null);
  128. }
  129. set {
  130. if (!value)
  131. Attributes.Remove ("disabled");
  132. else
  133. Attributes["disabled"] = "disabled";
  134. }
  135. }
  136. [Browsable(false)]
  137. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  138. public CssStyleCollection Style {
  139. get { return Attributes.CssStyle; }
  140. }
  141. [DefaultValue("")]
  142. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  143. [WebSysDescription("")]
  144. [WebCategory("Appearance")]
  145. public virtual string TagName {
  146. get { return _tagName; }
  147. }
  148. protected override bool ViewStateIgnoresCase {
  149. get {
  150. return true;
  151. }
  152. }
  153. }
  154. }