WebControl.cs 13 KB

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