WebControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. ViewState["AccessKey"] = value;
  56. }
  57. }
  58. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  59. [WebSysDescription ("Attribute tags for the Webcontrol.")]
  60. public AttributeCollection Attributes
  61. {
  62. get
  63. {
  64. if(attributes==null)
  65. {
  66. //FIXME: From where to get StateBag and how? I think this method is OK!
  67. if(attributeState == null)
  68. {
  69. attributeState = new StateBag(true);
  70. if(IsTrackingViewState)
  71. {
  72. attributeState.TrackViewState();
  73. }
  74. }
  75. attributes = new AttributeCollection(attributeState);
  76. }
  77. return attributes;
  78. }
  79. }
  80. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  81. [TypeConverter (typeof (WebColorConverter))]
  82. [WebSysDescription ("The background color for the WebControl.")]
  83. public virtual Color BackColor
  84. {
  85. get {
  86. if (!ControlStyleCreated)
  87. return Color.Empty;
  88. return ControlStyle.BackColor;
  89. }
  90. set {
  91. ControlStyle.BackColor = value;
  92. }
  93. }
  94. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  95. [TypeConverter (typeof (WebColorConverter))]
  96. [WebSysDescription ("The border color for the WebControl.")]
  97. public virtual Color BorderColor
  98. {
  99. get {
  100. if (!ControlStyleCreated)
  101. return Color.Empty;
  102. return ControlStyle.BorderColor;
  103. }
  104. set {
  105. ControlStyle.BorderColor = value;
  106. }
  107. }
  108. [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
  109. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  110. public virtual BorderStyle BorderStyle
  111. {
  112. get {
  113. if (!ControlStyleCreated)
  114. return BorderStyle.NotSet;
  115. return ControlStyle.BorderStyle;
  116. }
  117. set {
  118. ControlStyle.BorderStyle = value;
  119. }
  120. }
  121. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  122. [WebSysDescription ("The width of the border used for the WebControl.")]
  123. public virtual Unit BorderWidth
  124. {
  125. get {
  126. if (!ControlStyleCreated)
  127. return Unit.Empty;
  128. return ControlStyle.BorderWidth;
  129. }
  130. set {
  131. if (value.Value < 0)
  132. throw new ArgumentException();
  133. ControlStyle.BorderWidth = value;
  134. }
  135. }
  136. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  137. [WebSysDescription ("The style used to display this Webcontrol.")]
  138. public Style ControlStyle
  139. {
  140. get
  141. {
  142. if(controlStyle == null)
  143. {
  144. controlStyle = CreateControlStyle();
  145. if(IsTrackingViewState)
  146. {
  147. controlStyle.TrackViewState();
  148. }
  149. controlStyle.LoadViewState(null);
  150. }
  151. return controlStyle;
  152. }
  153. }
  154. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  155. [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
  156. public bool ControlStyleCreated
  157. {
  158. get
  159. {
  160. return (controlStyle!=null);
  161. }
  162. }
  163. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  164. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  165. public virtual string CssClass
  166. {
  167. get
  168. {
  169. return ControlStyle.CssClass;
  170. }
  171. set
  172. {
  173. ControlStyle.CssClass = value;
  174. }
  175. }
  176. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  177. [WebSysDescription ("The activation state of this WebControl.")]
  178. public virtual bool Enabled {
  179. get {
  180. if (!enabled)
  181. return false;
  182. WebControl parent = Parent as WebControl;
  183. if (parent != null)
  184. return ((WebControl) parent).Enabled;
  185. return true;
  186. }
  187. set {
  188. if (enabled != value)
  189. ViewState ["Enabled"] = value;
  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. ControlStyle.Height = value;
  228. }
  229. }
  230. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  231. [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
  232. public CssStyleCollection Style
  233. {
  234. get
  235. {
  236. return Attributes.CssStyle;
  237. }
  238. }
  239. [DefaultValue (0), WebCategory ("Behavior")]
  240. [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
  241. public virtual short TabIndex
  242. {
  243. get
  244. {
  245. object o = ViewState["TabIndex"];
  246. if(o!=null)
  247. return (short)o;
  248. return 0;
  249. }
  250. set
  251. {
  252. if(value < -32768 || value > 32767)
  253. throw new ArgumentException();
  254. ViewState["TabIndex"] = value;
  255. }
  256. }
  257. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  258. [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
  259. public virtual string ToolTip
  260. {
  261. get
  262. {
  263. object o = ViewState["ToolTip"];
  264. if(o!=null)
  265. return (string)o;
  266. return String.Empty;
  267. }
  268. set
  269. {
  270. ViewState["ToolTip"] = value;
  271. }
  272. }
  273. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  274. [WebSysDescription ("The width of this WebControl.")]
  275. public virtual Unit Width
  276. {
  277. get
  278. {
  279. return ControlStyle.Width;
  280. }
  281. set
  282. {
  283. ControlStyle.Width = value;
  284. }
  285. }
  286. public void ApplyStyle(Style s)
  287. {
  288. if (s != null && !s.IsEmpty)
  289. ControlStyle.CopyFrom (s);
  290. }
  291. public void CopyBaseAttributes(WebControl controlSrc)
  292. {
  293. /*
  294. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  295. */
  296. AccessKey = controlSrc.AccessKey;
  297. Enabled = controlSrc.Enabled;
  298. ToolTip = controlSrc.ToolTip;
  299. TabIndex = controlSrc.TabIndex;
  300. attributes = controlSrc.Attributes;
  301. AttributeCollection otherAtt = controlSrc.Attributes;
  302. foreach (string key in controlSrc.Attributes.Keys)
  303. Attributes [key] = otherAtt [key];
  304. }
  305. public void MergeStyle(Style s)
  306. {
  307. ControlStyle.MergeWith(s);
  308. }
  309. public virtual void RenderBeginTag(HtmlTextWriter writer)
  310. {
  311. AddAttributesToRender(writer);
  312. writer.RenderBeginTag(TagName);
  313. }
  314. public virtual void RenderEndTag(HtmlTextWriter writer)
  315. {
  316. writer.RenderEndTag();
  317. }
  318. protected virtual HtmlTextWriterTag TagKey
  319. {
  320. get
  321. {
  322. return tagKey;
  323. }
  324. }
  325. protected virtual string TagName
  326. {
  327. get
  328. {
  329. if(tagName == null && TagKey != 0)
  330. {
  331. tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
  332. }
  333. // What if tagName is null and tagKey 0?
  334. return tagName;
  335. }
  336. }
  337. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  338. {
  339. if(ID!=null)
  340. {
  341. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  342. }
  343. if(AccessKey.Length>0)
  344. {
  345. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  346. }
  347. if(!Enabled)
  348. {
  349. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  350. }
  351. if(ToolTip.Length>0)
  352. {
  353. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  354. }
  355. if(TabIndex != 0)
  356. {
  357. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  358. }
  359. if(ControlStyleCreated)
  360. {
  361. if(!ControlStyle.IsEmpty)
  362. {
  363. ControlStyle.AddAttributesToRender(writer, this);
  364. }
  365. }
  366. if(attributeState != null){
  367. IEnumerator ie = Attributes.Keys.GetEnumerator ();
  368. while (ie.MoveNext ()){
  369. string key = (string) ie.Current;
  370. writer.AddAttribute (key, Attributes [key]);
  371. }
  372. }
  373. }
  374. protected virtual Style CreateControlStyle ()
  375. {
  376. return new Style (ViewState);
  377. }
  378. protected override void LoadViewState (object savedState)
  379. {
  380. if (savedState == null)
  381. return;
  382. Triplet saved = (Triplet) savedState;
  383. base.LoadViewState (saved.First);
  384. if (saved.Second != null ||
  385. ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
  386. ControlStyle.LoadViewState (saved.Second);
  387. if (attributeState != null)
  388. attributeState.LoadViewState (saved.Third);
  389. object e = ViewState ["Enabled"];
  390. if (e != null)
  391. enabled = (bool) e;
  392. }
  393. protected override void Render(HtmlTextWriter writer)
  394. {
  395. RenderBeginTag (writer);
  396. RenderContents (writer);
  397. RenderEndTag (writer);
  398. }
  399. protected virtual void RenderContents(HtmlTextWriter writer)
  400. {
  401. base.Render (writer);
  402. }
  403. protected override object SaveViewState()
  404. {
  405. object controlView = null;
  406. if (ControlStyleCreated)
  407. controlView = ControlStyle.SaveViewState ();
  408. ViewState ["Enabled"] = enabled;
  409. object baseView = base.SaveViewState ();
  410. object attrView = null;
  411. if (attributeState != null)
  412. attrView = attributeState.SaveViewState ();
  413. return new Triplet (baseView, controlView, attrView);
  414. }
  415. protected override void TrackViewState()
  416. {
  417. base.TrackViewState();
  418. if (ControlStyleCreated)
  419. ControlStyle.TrackViewState ();
  420. if (attributeState != null)
  421. attributeState.TrackViewState ();
  422. }
  423. string IAttributeAccessor.GetAttribute(string key)
  424. {
  425. if(Attributes!=null)
  426. return Attributes[key] as string;
  427. return null;
  428. }
  429. void IAttributeAccessor.SetAttribute(string key, string value)
  430. {
  431. Attributes[key] = value;
  432. }
  433. }
  434. }