WebControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // System.Web.UI.WebControls.WebControl.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.Drawing;
  17. using System.Collections.Specialized;
  18. namespace System.Web.UI.WebControls
  19. {
  20. [PersistChildrenAttribute(false)]
  21. [ParseChildrenAttribute(true)]
  22. public class WebControl : Control, IAttributeAccessor
  23. {
  24. HtmlTextWriterTag tagKey;
  25. AttributeCollection attributes;
  26. StateBag attributeState;
  27. Style controlStyle;
  28. bool enabled = true;
  29. string tagName;
  30. // TODO: The constructors definitions
  31. protected WebControl () : this (HtmlTextWriterTag.Span)
  32. {
  33. }
  34. public WebControl (HtmlTextWriterTag tag)
  35. {
  36. tagKey = tag;
  37. }
  38. protected WebControl (string tag)
  39. {
  40. tagName = tag;
  41. }
  42. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  43. [WebSysDescription ("A keyboard shortcut for the WebControl.")]
  44. public virtual string AccessKey
  45. {
  46. get
  47. {
  48. object o = ViewState["AccessKey"];
  49. if(o!=null)
  50. return (string)o;
  51. return String.Empty;
  52. }
  53. set
  54. {
  55. if (value != null && value.Length > 1)
  56. throw new ArgumentOutOfRangeException ("value");
  57. ViewState["AccessKey"] = value;
  58. }
  59. }
  60. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  61. [WebSysDescription ("Attribute tags for the Webcontrol.")]
  62. public AttributeCollection Attributes
  63. {
  64. get
  65. {
  66. if(attributes==null)
  67. {
  68. //FIXME: From where to get StateBag and how? I think this method is OK!
  69. if(attributeState == null)
  70. {
  71. attributeState = new StateBag(true);
  72. if(IsTrackingViewState)
  73. {
  74. attributeState.TrackViewState();
  75. }
  76. }
  77. attributes = new AttributeCollection(attributeState);
  78. }
  79. return attributes;
  80. }
  81. }
  82. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  83. [TypeConverter (typeof (WebColorConverter))]
  84. [WebSysDescription ("The background color for the WebControl.")]
  85. public virtual Color BackColor
  86. {
  87. get {
  88. if (!ControlStyleCreated)
  89. return Color.Empty;
  90. return ControlStyle.BackColor;
  91. }
  92. set {
  93. ControlStyle.BackColor = value;
  94. }
  95. }
  96. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  97. [TypeConverter (typeof (WebColorConverter))]
  98. [WebSysDescription ("The border color for the WebControl.")]
  99. public virtual Color BorderColor
  100. {
  101. get {
  102. if (!ControlStyleCreated)
  103. return Color.Empty;
  104. return ControlStyle.BorderColor;
  105. }
  106. set {
  107. ControlStyle.BorderColor = value;
  108. }
  109. }
  110. [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
  111. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  112. public virtual BorderStyle BorderStyle
  113. {
  114. get {
  115. if (!ControlStyleCreated)
  116. return BorderStyle.NotSet;
  117. return ControlStyle.BorderStyle;
  118. }
  119. set {
  120. ControlStyle.BorderStyle = value;
  121. }
  122. }
  123. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  124. [WebSysDescription ("The width of the border used for the WebControl.")]
  125. public virtual Unit BorderWidth
  126. {
  127. get {
  128. if (!ControlStyleCreated)
  129. return Unit.Empty;
  130. return ControlStyle.BorderWidth;
  131. }
  132. set {
  133. if (value.Value < 0)
  134. throw new ArgumentOutOfRangeException ("value");
  135. ControlStyle.BorderWidth = value;
  136. }
  137. }
  138. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  139. [WebSysDescription ("The style used to display this Webcontrol.")]
  140. public Style ControlStyle
  141. {
  142. get
  143. {
  144. if(controlStyle == null)
  145. {
  146. controlStyle = CreateControlStyle();
  147. if(IsTrackingViewState)
  148. {
  149. controlStyle.TrackViewState();
  150. }
  151. controlStyle.LoadViewState(null);
  152. }
  153. return controlStyle;
  154. }
  155. }
  156. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  157. [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
  158. public bool ControlStyleCreated
  159. {
  160. get
  161. {
  162. return (controlStyle!=null);
  163. }
  164. }
  165. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  166. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  167. public virtual string CssClass
  168. {
  169. get
  170. {
  171. return ControlStyle.CssClass;
  172. }
  173. set
  174. {
  175. ControlStyle.CssClass = value;
  176. }
  177. }
  178. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  179. [WebSysDescription ("The activation state of this WebControl.")]
  180. public virtual bool Enabled {
  181. get {
  182. return enabled;
  183. }
  184. set {
  185. if (enabled != value) {
  186. ViewState ["Enabled"] = value;
  187. if (IsTrackingViewState)
  188. EnableViewState = true;
  189. }
  190. enabled = value;
  191. }
  192. }
  193. [DefaultValue (null), NotifyParentProperty (true), WebCategory ("Appearance")]
  194. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  195. [WebSysDescription ("The font of this WebControl.")]
  196. public virtual FontInfo Font
  197. {
  198. get
  199. {
  200. return ControlStyle.Font;
  201. }
  202. }
  203. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  204. [TypeConverter (typeof (WebColorConverter))]
  205. [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
  206. public virtual Color ForeColor
  207. {
  208. get {
  209. if (!ControlStyleCreated)
  210. return Color.Empty;
  211. return ControlStyle.ForeColor;
  212. }
  213. set {
  214. ControlStyle.ForeColor = value;
  215. }
  216. }
  217. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  218. [WebSysDescription ("The height of this WebControl.")]
  219. public virtual Unit Height
  220. {
  221. get
  222. {
  223. return ControlStyle.Height;
  224. }
  225. set
  226. {
  227. if (value.Value < 0)
  228. throw new ArgumentOutOfRangeException ("value");
  229. ControlStyle.Height = value;
  230. }
  231. }
  232. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  233. [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
  234. public CssStyleCollection Style
  235. {
  236. get
  237. {
  238. return Attributes.CssStyle;
  239. }
  240. }
  241. [DefaultValue (0), WebCategory ("Behavior")]
  242. [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
  243. public virtual short TabIndex
  244. {
  245. get
  246. {
  247. object o = ViewState["TabIndex"];
  248. if(o!=null)
  249. return (short)o;
  250. return 0;
  251. }
  252. set
  253. {
  254. if(value < short.MinValue || value > short.MaxValue)
  255. throw new ArgumentOutOfRangeException ("value");
  256. ViewState["TabIndex"] = value;
  257. }
  258. }
  259. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  260. [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
  261. public virtual string ToolTip
  262. {
  263. get
  264. {
  265. object o = ViewState["ToolTip"];
  266. if(o!=null)
  267. return (string)o;
  268. return String.Empty;
  269. }
  270. set
  271. {
  272. ViewState["ToolTip"] = value;
  273. }
  274. }
  275. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  276. [WebSysDescription ("The width of this WebControl.")]
  277. public virtual Unit Width
  278. {
  279. get
  280. {
  281. return ControlStyle.Width;
  282. }
  283. set
  284. {
  285. if (value.Value < 0)
  286. throw new ArgumentOutOfRangeException ("value");
  287. ControlStyle.Width = value;
  288. }
  289. }
  290. public void ApplyStyle(Style s)
  291. {
  292. if (s != null && !s.IsEmpty)
  293. ControlStyle.CopyFrom (s);
  294. }
  295. public void CopyBaseAttributes(WebControl controlSrc)
  296. {
  297. /*
  298. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  299. */
  300. AccessKey = controlSrc.AccessKey;
  301. Enabled = controlSrc.Enabled;
  302. ToolTip = controlSrc.ToolTip;
  303. TabIndex = controlSrc.TabIndex;
  304. AttributeCollection otherAtt = controlSrc.Attributes;
  305. foreach (string key in otherAtt.Keys)
  306. Attributes [key] = otherAtt [key];
  307. }
  308. public void MergeStyle(Style s)
  309. {
  310. ControlStyle.MergeWith(s);
  311. }
  312. public virtual void RenderBeginTag(HtmlTextWriter writer)
  313. {
  314. AddAttributesToRender(writer);
  315. writer.RenderBeginTag(TagName);
  316. }
  317. public virtual void RenderEndTag(HtmlTextWriter writer)
  318. {
  319. writer.RenderEndTag();
  320. }
  321. protected virtual HtmlTextWriterTag TagKey
  322. {
  323. get
  324. {
  325. return tagKey;
  326. }
  327. }
  328. protected virtual string TagName
  329. {
  330. get
  331. {
  332. if(tagName == null && TagKey != 0)
  333. {
  334. tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
  335. }
  336. // What if tagName is null and tagKey 0?
  337. return tagName;
  338. }
  339. }
  340. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  341. {
  342. if(ID!=null)
  343. {
  344. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  345. }
  346. if(AccessKey.Length>0)
  347. {
  348. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  349. }
  350. if(!Enabled)
  351. {
  352. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  353. }
  354. if(ToolTip.Length>0)
  355. {
  356. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  357. }
  358. if(TabIndex != 0)
  359. {
  360. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  361. }
  362. if(ControlStyleCreated)
  363. {
  364. if(!ControlStyle.IsEmpty)
  365. {
  366. ControlStyle.AddAttributesToRender(writer, this);
  367. }
  368. }
  369. if(attributeState != null){
  370. IEnumerator ie = Attributes.Keys.GetEnumerator ();
  371. while (ie.MoveNext ()){
  372. string key = (string) ie.Current;
  373. writer.AddAttribute (key, Attributes [key]);
  374. }
  375. }
  376. }
  377. protected virtual Style CreateControlStyle ()
  378. {
  379. return new Style (ViewState);
  380. }
  381. protected override void LoadViewState (object savedState)
  382. {
  383. if (savedState == null)
  384. return;
  385. Pair saved = (Pair) savedState;
  386. base.LoadViewState (saved.First);
  387. if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
  388. ControlStyle.LoadViewState (null);
  389. if (saved.Second != null)
  390. {
  391. attributeState = new StateBag(true);
  392. attributeState.TrackViewState();
  393. attributeState.LoadViewState (saved.Second);
  394. }
  395. }
  396. protected override void Render(HtmlTextWriter writer)
  397. {
  398. RenderBeginTag (writer);
  399. RenderContents (writer);
  400. RenderEndTag (writer);
  401. }
  402. protected virtual void RenderContents(HtmlTextWriter writer)
  403. {
  404. base.Render (writer);
  405. }
  406. protected override object SaveViewState()
  407. {
  408. if (ControlStyleCreated)
  409. ControlStyle.SaveViewState ();
  410. object baseView = base.SaveViewState ();
  411. object attrView = null;
  412. if (attributeState != null)
  413. attrView = attributeState.SaveViewState ();
  414. if (baseView == null && attrView == null)
  415. return null;
  416. return new Pair (baseView, attrView);
  417. }
  418. protected override void TrackViewState()
  419. {
  420. base.TrackViewState();
  421. if (ControlStyleCreated)
  422. ControlStyle.TrackViewState ();
  423. if (attributeState != null)
  424. attributeState.TrackViewState ();
  425. }
  426. string IAttributeAccessor.GetAttribute(string key)
  427. {
  428. if (attributes != null)
  429. return Attributes [key] as string;
  430. return null;
  431. }
  432. void IAttributeAccessor.SetAttribute(string key, string value)
  433. {
  434. Attributes [key] = value;
  435. }
  436. }
  437. }