HtmlControl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 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. private AttributeCollection _attributes;
  46. #if NET_2_0
  47. protected
  48. #else
  49. public
  50. #endif
  51. HtmlControl() : this ("span") {}
  52. #if NET_2_0
  53. protected
  54. #else
  55. public
  56. #endif
  57. HtmlControl(string tag)
  58. {
  59. _tagName = tag;
  60. }
  61. protected override ControlCollection CreateControlCollection ()
  62. {
  63. return new EmptyControlCollection (this);
  64. }
  65. internal static string AttributeToString(int n){
  66. if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
  67. return null;
  68. }
  69. internal static string AttributeToString(string s){
  70. if (s != null && s.Length != 0) return s;
  71. return null;
  72. }
  73. internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
  74. string attr = Attributes[attribName];
  75. if (attr != null){
  76. if (attr.Length != 0){
  77. try{
  78. attr = ResolveUrl(attr);
  79. }
  80. catch (Exception) {
  81. throw new HttpException(attribName + " property had malformed url");
  82. }
  83. writer.WriteAttribute(attribName, attr);
  84. Attributes.Remove(attribName);
  85. }
  86. }
  87. }
  88. #if NET_2_0
  89. /* keep these two methods in sync with the
  90. * IAttributeAccessor iface methods below */
  91. protected virtual string GetAttribute (string name)
  92. {
  93. return Attributes[name];
  94. }
  95. protected virtual void SetAttribute (string name, string value)
  96. {
  97. Attributes[name] = value;
  98. }
  99. #endif
  100. string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
  101. return Attributes[name];
  102. }
  103. void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
  104. Attributes[name] = value;
  105. }
  106. protected virtual void RenderBeginTag (HtmlTextWriter writer)
  107. {
  108. writer.WriteBeginTag (TagName);
  109. RenderAttributes (writer);
  110. writer.Write ('>');
  111. }
  112. #if NET_2_0
  113. protected internal
  114. #else
  115. protected
  116. #endif
  117. override void Render (HtmlTextWriter writer)
  118. {
  119. RenderBeginTag (writer);
  120. }
  121. protected virtual void RenderAttributes(HtmlTextWriter writer){
  122. if (ID != null){
  123. writer.WriteAttribute("id",ClientID);
  124. }
  125. Attributes.Render(writer);
  126. }
  127. [Browsable(false)]
  128. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  129. public AttributeCollection Attributes
  130. {
  131. get {
  132. if (_attributes == null)
  133. _attributes = new AttributeCollection (ViewState);
  134. return _attributes;
  135. }
  136. }
  137. [DefaultValue(false)]
  138. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  139. [WebSysDescription("")]
  140. [WebCategory("Behavior")]
  141. #if NET_2_0
  142. [TypeConverter (typeof(MinimizableAttributeTypeConverter))]
  143. #endif
  144. public bool Disabled
  145. {
  146. get {
  147. string disableAttr = Attributes["disabled"] as string;
  148. return (disableAttr != null);
  149. }
  150. set {
  151. if (!value)
  152. Attributes.Remove ("disabled");
  153. else
  154. Attributes["disabled"] = "disabled";
  155. }
  156. }
  157. [Browsable(false)]
  158. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  159. public CssStyleCollection Style
  160. {
  161. get { return Attributes.CssStyle; }
  162. }
  163. [DefaultValue("")]
  164. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  165. [WebSysDescription("")]
  166. [WebCategory("Appearance")]
  167. public virtual string TagName
  168. {
  169. get { return _tagName; }
  170. }
  171. protected override bool ViewStateIgnoresCase
  172. {
  173. get {
  174. return true;
  175. }
  176. }
  177. }
  178. }