WebControl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Drawing;
  37. using System.Collections.Specialized;
  38. namespace System.Web.UI.WebControls
  39. {
  40. #if NET_2_0
  41. [ThemeableAttribute (true)]
  42. #endif
  43. [PersistChildrenAttribute(false)]
  44. [ParseChildrenAttribute(true)]
  45. public class WebControl : Control, IAttributeAccessor
  46. {
  47. HtmlTextWriterTag tagKey;
  48. AttributeCollection attributes;
  49. StateBag attributeState;
  50. Style controlStyle;
  51. bool enabled = true;
  52. string tagName;
  53. protected WebControl () : this (HtmlTextWriterTag.Span)
  54. {
  55. }
  56. public WebControl (HtmlTextWriterTag tag)
  57. {
  58. tagKey = tag;
  59. }
  60. protected WebControl (string tag)
  61. {
  62. tagName = tag;
  63. }
  64. #if NET_2_0
  65. [Localizable (true)]
  66. #else
  67. [Bindable (true)]
  68. #endif
  69. [DefaultValue (""), WebCategory ("Behavior")]
  70. [WebSysDescription ("A keyboard shortcut for the WebControl.")]
  71. public virtual string AccessKey
  72. {
  73. get
  74. {
  75. object o = ViewState["AccessKey"];
  76. if(o!=null)
  77. return (string)o;
  78. return String.Empty;
  79. }
  80. set
  81. {
  82. if (value != null && value.Length > 1)
  83. throw new ArgumentOutOfRangeException ("value");
  84. ViewState["AccessKey"] = value;
  85. }
  86. }
  87. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  88. [WebSysDescription ("Attribute tags for the Webcontrol.")]
  89. public AttributeCollection Attributes
  90. {
  91. get
  92. {
  93. if(attributes==null)
  94. {
  95. //FIXME: From where to get StateBag and how? I think this method is OK!
  96. if(attributeState == null)
  97. {
  98. attributeState = new StateBag(true);
  99. if(IsTrackingViewState)
  100. {
  101. attributeState.TrackViewState();
  102. }
  103. }
  104. attributes = new AttributeCollection(attributeState);
  105. }
  106. return attributes;
  107. }
  108. }
  109. #if !NET_2_0
  110. [Bindable (true)]
  111. #endif
  112. [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
  113. [TypeConverter (typeof (WebColorConverter))]
  114. [WebSysDescription ("The background color for the WebControl.")]
  115. public virtual Color BackColor
  116. {
  117. get {
  118. if (!ControlStyleCreated)
  119. return Color.Empty;
  120. return ControlStyle.BackColor;
  121. }
  122. set {
  123. ControlStyle.BackColor = value;
  124. }
  125. }
  126. #if !NET_2_0
  127. [Bindable (true)]
  128. #endif
  129. [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
  130. [TypeConverter (typeof (WebColorConverter))]
  131. [WebSysDescription ("The border color for the WebControl.")]
  132. public virtual Color BorderColor
  133. {
  134. get {
  135. if (!ControlStyleCreated)
  136. return Color.Empty;
  137. return ControlStyle.BorderColor;
  138. }
  139. set {
  140. ControlStyle.BorderColor = value;
  141. }
  142. }
  143. #if !NET_2_0
  144. [Bindable (true)]
  145. #endif
  146. [DefaultValue (typeof(BorderStyle), "NotSet"), WebCategory ("Appearance")]
  147. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  148. public virtual BorderStyle BorderStyle
  149. {
  150. get {
  151. if (!ControlStyleCreated)
  152. return BorderStyle.NotSet;
  153. return ControlStyle.BorderStyle;
  154. }
  155. set {
  156. ControlStyle.BorderStyle = value;
  157. }
  158. }
  159. #if !NET_2_0
  160. [Bindable (true)]
  161. #endif
  162. [DefaultValue (typeof (Unit), ""), WebCategory ("Appearance")]
  163. [WebSysDescription ("The width of the border used for the WebControl.")]
  164. public virtual Unit BorderWidth
  165. {
  166. get {
  167. if (!ControlStyleCreated)
  168. return Unit.Empty;
  169. return ControlStyle.BorderWidth;
  170. }
  171. set {
  172. if (value.Value < 0)
  173. throw new ArgumentOutOfRangeException ("value");
  174. ControlStyle.BorderWidth = value;
  175. }
  176. }
  177. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  178. [WebSysDescription ("The style used to display this Webcontrol.")]
  179. public Style ControlStyle
  180. {
  181. get
  182. {
  183. if(controlStyle == null)
  184. {
  185. controlStyle = CreateControlStyle();
  186. if(IsTrackingViewState)
  187. {
  188. controlStyle.TrackViewState();
  189. }
  190. controlStyle.LoadViewState(null);
  191. }
  192. return controlStyle;
  193. }
  194. }
  195. #if NET_2_0
  196. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  197. #endif
  198. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  199. [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
  200. public bool ControlStyleCreated
  201. {
  202. get
  203. {
  204. return (controlStyle!=null);
  205. }
  206. }
  207. #if !NET_2_0
  208. [Bindable (true)]
  209. #endif
  210. [DefaultValue (""), WebCategory ("Appearance")]
  211. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  212. public virtual string CssClass
  213. {
  214. get
  215. {
  216. return ControlStyle.CssClass;
  217. }
  218. set
  219. {
  220. ControlStyle.CssClass = value;
  221. }
  222. }
  223. #if NET_2_0
  224. [ThemeableAttribute (false)]
  225. #endif
  226. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  227. [WebSysDescription ("The activation state of this WebControl.")]
  228. public virtual bool Enabled {
  229. get {
  230. return enabled;
  231. }
  232. set {
  233. if (enabled != value) {
  234. ViewState ["Enabled"] = value;
  235. if (IsTrackingViewState)
  236. EnableViewState = true;
  237. }
  238. enabled = value;
  239. }
  240. }
  241. #if !NET_2_0
  242. [DefaultValue (null)]
  243. #endif
  244. [NotifyParentProperty (true), WebCategory ("Appearance")]
  245. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  246. [WebSysDescription ("The font of this WebControl.")]
  247. public virtual FontInfo Font
  248. {
  249. get
  250. {
  251. return ControlStyle.Font;
  252. }
  253. }
  254. #if !NET_2_0
  255. [Bindable (true)]
  256. #endif
  257. [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
  258. [TypeConverter (typeof (WebColorConverter))]
  259. [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
  260. public virtual Color ForeColor
  261. {
  262. get {
  263. if (!ControlStyleCreated)
  264. return Color.Empty;
  265. return ControlStyle.ForeColor;
  266. }
  267. set {
  268. ControlStyle.ForeColor = value;
  269. }
  270. }
  271. #if !NET_2_0
  272. [Bindable (true)]
  273. #endif
  274. [DefaultValue (typeof(Unit), ""), WebCategory ("Layout")]
  275. [WebSysDescription ("The height of this WebControl.")]
  276. public virtual Unit Height
  277. {
  278. get
  279. {
  280. return ControlStyle.Height;
  281. }
  282. set
  283. {
  284. if (value.Value < 0)
  285. throw new ArgumentOutOfRangeException ("value");
  286. ControlStyle.Height = value;
  287. }
  288. }
  289. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  290. [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
  291. public CssStyleCollection Style
  292. {
  293. get
  294. {
  295. return Attributes.CssStyle;
  296. }
  297. }
  298. [DefaultValue (typeof (short), "0"), WebCategory ("Behavior")]
  299. [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
  300. public virtual short TabIndex
  301. {
  302. get
  303. {
  304. object o = ViewState["TabIndex"];
  305. if(o!=null)
  306. return (short)o;
  307. return 0;
  308. }
  309. set
  310. {
  311. if(value < short.MinValue || value > short.MaxValue)
  312. throw new ArgumentOutOfRangeException ("value");
  313. ViewState["TabIndex"] = value;
  314. }
  315. }
  316. #if NET_2_0
  317. [Localizable (true)]
  318. #else
  319. [Bindable (true)]
  320. #endif
  321. [DefaultValue (""), WebCategory ("Behavior")]
  322. [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
  323. public virtual string ToolTip
  324. {
  325. get
  326. {
  327. object o = ViewState["ToolTip"];
  328. if(o!=null)
  329. return (string)o;
  330. return String.Empty;
  331. }
  332. set
  333. {
  334. ViewState["ToolTip"] = value;
  335. }
  336. }
  337. #if !NET_2_0
  338. [Bindable (true)]
  339. #endif
  340. [DefaultValue ( typeof (Unit), ""), WebCategory ("Layout")]
  341. [WebSysDescription ("The width of this WebControl.")]
  342. public virtual Unit Width
  343. {
  344. get
  345. {
  346. return ControlStyle.Width;
  347. }
  348. set
  349. {
  350. if (value.Value < 0)
  351. throw new ArgumentOutOfRangeException ("value");
  352. ControlStyle.Width = value;
  353. }
  354. }
  355. public void ApplyStyle(Style s)
  356. {
  357. if (s != null && !s.IsEmpty)
  358. ControlStyle.CopyFrom (s);
  359. }
  360. public void CopyBaseAttributes(WebControl controlSrc)
  361. {
  362. /*
  363. * AccessKey, Enabled, ToolTip, TabIndex, Attributes
  364. */
  365. AccessKey = controlSrc.AccessKey;
  366. Enabled = controlSrc.Enabled;
  367. ToolTip = controlSrc.ToolTip;
  368. TabIndex = controlSrc.TabIndex;
  369. AttributeCollection otherAtt = controlSrc.Attributes;
  370. foreach (string key in otherAtt.Keys)
  371. Attributes [key] = otherAtt [key];
  372. }
  373. public void MergeStyle(Style s)
  374. {
  375. ControlStyle.MergeWith(s);
  376. }
  377. public virtual void RenderBeginTag (HtmlTextWriter writer)
  378. {
  379. AddAttributesToRender (writer);
  380. HtmlTextWriterTag tkey = TagKey;
  381. // The tagkey goes takes precedence if TagKey != 0 and TagName != null
  382. if (tkey != 0)
  383. writer.RenderBeginTag (tkey);
  384. else
  385. writer.RenderBeginTag (TagName);
  386. }
  387. public virtual void RenderEndTag(HtmlTextWriter writer)
  388. {
  389. writer.RenderEndTag();
  390. }
  391. [Browsable (false)]
  392. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  393. protected virtual HtmlTextWriterTag TagKey
  394. {
  395. get
  396. {
  397. return tagKey;
  398. }
  399. }
  400. [Browsable (false)]
  401. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  402. protected virtual string TagName
  403. {
  404. get
  405. {
  406. if(tagName == null && TagKey != 0)
  407. {
  408. tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
  409. }
  410. // What if tagName is null and tagKey 0?
  411. // Got the answer: nothing is rendered, empty, null
  412. return tagName;
  413. }
  414. }
  415. protected virtual void AddAttributesToRender(HtmlTextWriter writer)
  416. {
  417. if(ID!=null)
  418. {
  419. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  420. }
  421. if(AccessKey.Length>0)
  422. {
  423. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  424. }
  425. if(!Enabled)
  426. {
  427. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  428. }
  429. if(ToolTip.Length>0)
  430. {
  431. writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
  432. }
  433. if(TabIndex != 0)
  434. {
  435. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
  436. }
  437. if(ControlStyleCreated)
  438. {
  439. if(!ControlStyle.IsEmpty)
  440. {
  441. ControlStyle.AddAttributesToRender(writer, this);
  442. }
  443. }
  444. if(attributeState != null){
  445. IEnumerator ie = Attributes.Keys.GetEnumerator ();
  446. while (ie.MoveNext ()){
  447. string key = (string) ie.Current;
  448. writer.AddAttribute (key, Attributes [key]);
  449. }
  450. }
  451. }
  452. protected virtual Style CreateControlStyle ()
  453. {
  454. return new Style (ViewState);
  455. }
  456. protected override void LoadViewState (object savedState)
  457. {
  458. if (savedState == null)
  459. return;
  460. Pair saved = (Pair) savedState;
  461. base.LoadViewState (saved.First);
  462. if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
  463. ControlStyle.LoadViewState (null);
  464. if (saved.Second != null)
  465. {
  466. if (attributeState == null)
  467. {
  468. attributeState = new StateBag(true);
  469. attributeState.TrackViewState();
  470. }
  471. attributeState.LoadViewState (saved.Second);
  472. }
  473. object enable = ViewState["Enabled"];
  474. if (enable!=null)
  475. {
  476. Enabled = (bool)enable;
  477. EnableViewState = true;
  478. }
  479. }
  480. protected override void Render(HtmlTextWriter writer)
  481. {
  482. RenderBeginTag (writer);
  483. RenderContents (writer);
  484. RenderEndTag (writer);
  485. }
  486. protected virtual void RenderContents(HtmlTextWriter writer)
  487. {
  488. base.Render (writer);
  489. }
  490. protected override object SaveViewState()
  491. {
  492. if (EnableViewState)
  493. ViewState["Enabled"] = enabled;
  494. if (ControlStyleCreated)
  495. ControlStyle.SaveViewState ();
  496. object baseView = base.SaveViewState ();
  497. object attrView = null;
  498. if (attributeState != null)
  499. attrView = attributeState.SaveViewState ();
  500. if (baseView == null && attrView == null)
  501. return null;
  502. return new Pair (baseView, attrView);
  503. }
  504. protected override void TrackViewState()
  505. {
  506. base.TrackViewState();
  507. if (ControlStyleCreated)
  508. ControlStyle.TrackViewState ();
  509. if (attributeState != null)
  510. attributeState.TrackViewState ();
  511. }
  512. string IAttributeAccessor.GetAttribute(string key)
  513. {
  514. if (attributes != null)
  515. return Attributes [key] as string;
  516. return null;
  517. }
  518. void IAttributeAccessor.SetAttribute(string key, string value)
  519. {
  520. Attributes [key] = value;
  521. }
  522. }
  523. }