1 //------------------------------------------------------------------------------ 2 // <copyright file="HtmlControl.cs" company="Microsoft"> 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // </copyright> 5 //------------------------------------------------------------------------------ 6 7 /* 8 * HtmlControl.cs 9 * 10 * Copyright (c) 2000 Microsoft Corporation 11 */ 12 13 namespace System.Web.UI.HtmlControls { 14 using System; 15 using System.Globalization; 16 using System.Collections; 17 using System.ComponentModel; 18 using System.ComponentModel.Design; 19 using System.IO; 20 using System.Web.Util; 21 using System.Web.UI; 22 using AttributeCollection = System.Web.UI.AttributeCollection; 23 using System.Security.Permissions; 24 25 /* 26 * An abstract base class representing an intrinsic Html tag that 27 * is not represented by both a begin and end tag, for example 28 * INPUT or IMG. 29 */ 30 31 /// <devdoc> 32 /// <para> 33 /// The <see langword='HtmlControl'/> 34 /// class defines the methods, properties, and events 35 /// common to all HTML Server controls in the Web Forms page framework. 36 /// </para> 37 /// </devdoc> 38 [ 39 Designer("System.Web.UI.Design.HtmlIntrinsicControlDesigner, " + AssemblyRef.SystemDesign), 40 ToolboxItem(false) 41 ] 42 abstract public class HtmlControl : Control, IAttributeAccessor { 43 internal string _tagName; 44 private AttributeCollection _attributes; 45 46 47 48 /// <devdoc> 49 /// </devdoc> HtmlControl()50 protected HtmlControl() : this("span") { 51 } 52 53 54 /// <devdoc> 55 /// </devdoc> HtmlControl(string tag)56 protected HtmlControl(string tag) { 57 _tagName = tag; 58 } 59 60 61 /* 62 * Access to collection of Attributes. 63 */ 64 65 /// <devdoc> 66 /// <para> 67 /// Gets all attribute name/value pairs expressed on a 68 /// server control tag within a selected ASP.NET page. 69 /// </para> 70 /// </devdoc> 71 [ 72 Browsable(false), 73 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 74 ] 75 public AttributeCollection Attributes { 76 get { 77 if (_attributes == null) 78 _attributes = new AttributeCollection(ViewState); 79 80 return _attributes; 81 } 82 } 83 84 85 /* 86 * Access to collection of styles. 87 */ 88 89 /// <devdoc> 90 /// <para> 91 /// Gets all 92 /// cascading style sheet (CSS) properties that 93 /// are applied 94 /// to a specified HTML Server control in an .aspx 95 /// file. 96 /// </para> 97 /// </devdoc> 98 [ 99 Browsable(false), 100 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 101 ] 102 public CssStyleCollection Style { 103 get { 104 return Attributes.CssStyle; 105 } 106 } 107 108 /* 109 * Property to get name of tag. 110 */ 111 112 /// <devdoc> 113 /// <para> 114 /// Gets the element name of a tag that contains a runat=server 115 /// attribute/value pair. 116 /// </para> 117 /// </devdoc> 118 [ 119 WebCategory("Appearance"), 120 DefaultValue(""), 121 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) 122 ] 123 public virtual string TagName { 124 get { return _tagName;} 125 } 126 127 /* 128 * Disabled property. 129 */ 130 131 /// <devdoc> 132 /// <para> 133 /// Gets or sets 134 /// a value indicating whether ---- attribute is included when a server 135 /// control is rendered. 136 /// </para> 137 /// </devdoc> 138 [ 139 WebCategory("Behavior"), 140 DefaultValue(false), 141 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 142 TypeConverter(typeof(MinimizableAttributeTypeConverter)) 143 ] 144 public bool Disabled { 145 get { 146 string s = Attributes["disabled"]; 147 return((s != null) ? (s.Equals("disabled")) : false); 148 } 149 150 set { 151 if (value) 152 Attributes["disabled"] = "disabled"; 153 else 154 Attributes["disabled"] = null; 155 156 } 157 } 158 159 160 /// <devdoc> 161 /// </devdoc> 162 /// <internalonly/> 163 protected override bool ViewStateIgnoresCase { 164 get { 165 return true; 166 } 167 } 168 169 170 /// <devdoc> 171 /// <para>[To be supplied.]</para> 172 /// </devdoc> CreateControlCollection()173 protected override ControlCollection CreateControlCollection() { 174 return new EmptyControlCollection(this); 175 } 176 177 /* 178 * Render the control into the given writer. 179 */ 180 181 /// <internalonly/> 182 /// <devdoc> 183 /// </devdoc> Render(HtmlTextWriter writer)184 protected internal override void Render(HtmlTextWriter writer) { 185 RenderBeginTag(writer); 186 } 187 188 /* 189 * Render only the attributes, attr1=value1 attr2=value2 ... 190 */ 191 192 /// <internalonly/> 193 /// <devdoc> 194 /// </devdoc> RenderAttributes(HtmlTextWriter writer)195 protected virtual void RenderAttributes(HtmlTextWriter writer) { 196 if (ID != null) 197 writer.WriteAttribute("id", ClientID); 198 199 Attributes.Render(writer); 200 } 201 202 /* 203 * Render the begin tag and its attributes, <TAGNAME attr1=value1 attr2=value2>. 204 */ 205 206 /// <internalonly/> 207 /// <devdoc> 208 /// </devdoc> RenderBeginTag(HtmlTextWriter writer)209 protected virtual void RenderBeginTag(HtmlTextWriter writer) { 210 writer.WriteBeginTag(TagName); 211 RenderAttributes(writer); 212 writer.Write(HtmlTextWriter.TagRightChar); 213 } 214 215 /* 216 * HtmlControls support generic access to Attributes. 217 */ 218 219 /// <internalonly/> 220 /// <devdoc> 221 /// </devdoc> IAttributeAccessor.GetAttribute(string name)222 string IAttributeAccessor.GetAttribute(string name) { 223 return GetAttribute(name); 224 } 225 226 227 /// <internalonly/> 228 /// <devdoc> 229 /// </devdoc> GetAttribute(string name)230 protected virtual string GetAttribute(string name) { 231 return Attributes[name]; 232 } 233 234 /* 235 * HtmlControls support generic access to Attributes. 236 */ 237 238 /// <internalonly/> 239 /// <devdoc> 240 /// </devdoc> IAttributeAccessor.SetAttribute(string name, string value)241 void IAttributeAccessor.SetAttribute(string name, string value) { 242 SetAttribute(name, value); 243 } 244 245 246 /// <internalonly/> 247 /// <devdoc> 248 /// </devdoc> SetAttribute(string name, string value)249 protected virtual void SetAttribute(string name, string value) { 250 Attributes[name] = value; 251 } 252 PreProcessRelativeReferenceAttribute(HtmlTextWriter writer, string attribName)253 internal void PreProcessRelativeReferenceAttribute(HtmlTextWriter writer, 254 string attribName) { 255 256 string url = Attributes[attribName]; 257 258 // Don't do anything if it's not specified 259 if (String.IsNullOrEmpty(url)) 260 return; 261 262 try { 263 url = ResolveClientUrl(url); 264 } 265 catch (Exception e) { 266 throw new HttpException(SR.GetString(SR.Property_Had_Malformed_Url, attribName, e.Message)); 267 } 268 269 writer.WriteAttribute(attribName, url); 270 Attributes.Remove(attribName); 271 } 272 MapStringAttributeToString(string s)273 internal static string MapStringAttributeToString(string s) { 274 275 // If it's an empty string, change it to null 276 if (s != null && s.Length == 0) 277 return null; 278 279 // Otherwise, just return the input 280 return s; 281 } 282 MapIntegerAttributeToString(int n)283 internal static string MapIntegerAttributeToString(int n) { 284 285 // If it's -1, change it to null 286 if (n == -1) 287 return null; 288 289 // Otherwise, convert the integer to a string 290 return n.ToString(NumberFormatInfo.InvariantInfo); 291 } 292 } 293 } 294 295