WebControl.cs 11 KB

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