WebControl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. using System.Security.Permissions;
  29. namespace System.Web.UI.WebControls {
  30. // CAS
  31. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  32. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. // attributes
  34. [ParseChildren (true)]
  35. #if NET_2_0
  36. [PersistChildrenAttribute (false, false)]
  37. [Themeable (true)]
  38. #else
  39. [PersistChildrenAttribute (false)]
  40. #endif
  41. public class WebControl : Control, IAttributeAccessor
  42. {
  43. #if NET_4_0
  44. const string DEFAULT_DISABLED_CSS_CLASS = "aspNetDisabled";
  45. #endif
  46. Style style;
  47. HtmlTextWriterTag tag;
  48. string tag_name;
  49. AttributeCollection attributes;
  50. StateBag attribute_state;
  51. bool enabled;
  52. bool track_enabled_state;
  53. #if NET_4_0
  54. static WebControl ()
  55. {
  56. DisabledCssClass = DEFAULT_DISABLED_CSS_CLASS;
  57. }
  58. #endif
  59. public WebControl (HtmlTextWriterTag tag)
  60. {
  61. this.tag = tag;
  62. this.enabled = true;
  63. }
  64. protected WebControl () : this (HtmlTextWriterTag.Span)
  65. {
  66. }
  67. protected WebControl (string tag)
  68. {
  69. this.tag = HtmlTextWriterTag.Unknown;
  70. this.tag_name = tag;
  71. this.enabled = true;
  72. }
  73. #if ONLY_1_1
  74. [Bindable(true)]
  75. #endif
  76. [DefaultValue("")]
  77. [WebSysDescription ("")]
  78. [WebCategory ("Behavior")]
  79. public virtual string AccessKey {
  80. get {
  81. return ViewState.GetString ("AccessKey", string.Empty);
  82. }
  83. set {
  84. if (value == null || value.Length < 2)
  85. ViewState ["AccessKey"] = value;
  86. else
  87. throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
  88. }
  89. }
  90. [Browsable(false)]
  91. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  92. [WebSysDescription ("")]
  93. [WebCategory ("Behavior")]
  94. public AttributeCollection Attributes {
  95. get {
  96. if (attributes == null) {
  97. attribute_state = new StateBag (true);
  98. if (IsTrackingViewState)
  99. attribute_state.TrackViewState ();
  100. attributes = new AttributeCollection (attribute_state);
  101. }
  102. return attributes;
  103. }
  104. }
  105. #if ONLY_1_1
  106. [Bindable(true)]
  107. #endif
  108. [DefaultValue(typeof (Color), "")]
  109. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  110. [WebSysDescription ("")]
  111. [WebCategory ("Appearance")]
  112. public virtual Color BackColor {
  113. get {
  114. if (style == null)
  115. return Color.Empty;
  116. return style.BackColor;
  117. }
  118. set {
  119. ControlStyle.BackColor = value;
  120. }
  121. }
  122. #if ONLY_1_1
  123. [Bindable(true)]
  124. #endif
  125. [DefaultValue(typeof (Color), "")]
  126. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  127. [WebSysDescription ("")]
  128. [WebCategory ("Appearance")]
  129. public virtual Color BorderColor {
  130. get {
  131. if (style == null)
  132. return Color.Empty;
  133. return style.BorderColor;
  134. }
  135. set {
  136. ControlStyle.BorderColor = value;
  137. }
  138. }
  139. #if ONLY_1_1
  140. [Bindable(true)]
  141. #endif
  142. [DefaultValue(BorderStyle.NotSet)]
  143. [WebSysDescription ("")]
  144. [WebCategory ("Appearance")]
  145. public virtual BorderStyle BorderStyle {
  146. get {
  147. if (style == null)
  148. return BorderStyle.NotSet;
  149. return style.BorderStyle;
  150. }
  151. set {
  152. if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
  153. throw new ArgumentOutOfRangeException ("value");
  154. ControlStyle.BorderStyle = value;
  155. }
  156. }
  157. #if ONLY_1_1
  158. [Bindable(true)]
  159. #endif
  160. [DefaultValue(typeof (Unit), "")]
  161. [WebSysDescription ("")]
  162. [WebCategory ("Appearance")]
  163. public virtual Unit BorderWidth {
  164. get {
  165. if (style == null)
  166. return Unit.Empty;
  167. return style.BorderWidth;
  168. }
  169. set { ControlStyle.BorderWidth = value; }
  170. }
  171. [Browsable(false)]
  172. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  173. [WebSysDescription ("")]
  174. [WebCategory ("Appearance")]
  175. public Style ControlStyle {
  176. get {
  177. if (style == null) {
  178. style = this.CreateControlStyle ();
  179. if (IsTrackingViewState)
  180. style.TrackViewState ();
  181. }
  182. return style;
  183. }
  184. }
  185. [Browsable(false)]
  186. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  187. #if NET_2_0
  188. [EditorBrowsable (EditorBrowsableState.Never)]
  189. #endif
  190. public bool ControlStyleCreated {
  191. get {
  192. return style != null;
  193. }
  194. }
  195. #if ONLY_1_1
  196. [Bindable(true)]
  197. #endif
  198. [DefaultValue("")]
  199. [WebSysDescription ("")]
  200. [WebCategory ("Appearance")]
  201. public virtual string CssClass {
  202. get {
  203. if (style == null)
  204. return string.Empty;
  205. return style.CssClass;
  206. }
  207. set {
  208. ControlStyle.CssClass = value;
  209. }
  210. }
  211. [Bindable(true)]
  212. [DefaultValue(true)]
  213. #if NET_2_0
  214. [Themeable (false)]
  215. #endif
  216. public virtual bool Enabled {
  217. get {
  218. return enabled;
  219. }
  220. set {
  221. if (enabled != value) {
  222. if (IsTrackingViewState)
  223. track_enabled_state = true;
  224. enabled = value;
  225. }
  226. }
  227. }
  228. #if NET_2_0
  229. [Browsable (true)]
  230. public virtual new bool EnableTheming
  231. {
  232. get { return base.EnableTheming; }
  233. set { base.EnableTheming = value; }
  234. }
  235. #endif
  236. #if ONLY_1_1
  237. [DefaultValue(null)]
  238. #endif
  239. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  240. [NotifyParentProperty(true)]
  241. [WebSysDescription ("")]
  242. [WebCategory ("Appearance")]
  243. public virtual FontInfo Font {
  244. get {
  245. // Oddly enough, it looks like we have to let it create the style
  246. // since we can't create a FontInfo without a style owner
  247. return ControlStyle.Font;
  248. }
  249. }
  250. #if ONLY_1_1
  251. [Bindable(true)]
  252. #endif
  253. [DefaultValue(typeof (Color), "")]
  254. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  255. [WebSysDescription ("")]
  256. [WebCategory ("Appearance")]
  257. public virtual Color ForeColor {
  258. get {
  259. if (style == null)
  260. return Color.Empty;
  261. return style.ForeColor;
  262. }
  263. set {
  264. ControlStyle.ForeColor = value;
  265. }
  266. }
  267. [Browsable (false)]
  268. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  269. #if NET_2_0
  270. public
  271. #else
  272. internal
  273. #endif
  274. bool HasAttributes
  275. {
  276. get {
  277. return (attributes != null && attributes.Count > 0);
  278. }
  279. }
  280. #if ONLY_1_1
  281. [Bindable(true)]
  282. #endif
  283. [DefaultValue(typeof (Unit), "")]
  284. [WebSysDescription ("")]
  285. [WebCategory ("Layout")]
  286. public virtual Unit Height {
  287. get {
  288. if (style == null)
  289. return Unit.Empty;
  290. return style.Height;
  291. }
  292. set {
  293. ControlStyle.Height = value;
  294. }
  295. }
  296. #if NET_2_0
  297. [Browsable (true)]
  298. public virtual new string SkinID
  299. {
  300. get { return base.SkinID; }
  301. set { base.SkinID = value; }
  302. }
  303. #endif
  304. [Browsable(false)]
  305. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  306. [WebSysDescription ("")]
  307. [WebCategory ("Style")]
  308. public CssStyleCollection Style {
  309. get {
  310. return Attributes.CssStyle;
  311. }
  312. }
  313. [DefaultValue((short)0)]
  314. [WebSysDescription ("")]
  315. [WebCategory ("Behavior")]
  316. public virtual short TabIndex {
  317. get {
  318. return ViewState.GetShort ("TabIndex", 0);
  319. }
  320. set {
  321. ViewState ["TabIndex"] = value;
  322. }
  323. }
  324. #if ONLY_1_1
  325. [Bindable(true)]
  326. #endif
  327. [DefaultValue("")]
  328. #if NET_2_0
  329. [Localizable (true)]
  330. #endif
  331. [WebSysDescription ("")]
  332. [WebCategory ("Behavior")]
  333. public virtual string ToolTip {
  334. get {
  335. return ViewState.GetString ("ToolTip", string.Empty);
  336. }
  337. set {
  338. ViewState ["ToolTip"] = value;
  339. }
  340. }
  341. #if ONLY_1_1
  342. [Bindable(true)]
  343. #endif
  344. [DefaultValue(typeof (Unit), "")]
  345. [WebSysDescription ("")]
  346. [WebCategory ("Layout")]
  347. public virtual Unit Width {
  348. get {
  349. if (style == null)
  350. return Unit.Empty;
  351. return style.Width;
  352. }
  353. set {
  354. ControlStyle.Width = value;
  355. }
  356. }
  357. [Browsable(false)]
  358. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  359. protected virtual HtmlTextWriterTag TagKey {
  360. get {
  361. return tag;
  362. }
  363. }
  364. [Browsable(false)]
  365. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  366. protected virtual string TagName {
  367. get {
  368. // do this here to avoid potentially costly lookups on every control
  369. if (tag_name == null)
  370. tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
  371. return tag_name;
  372. }
  373. }
  374. #if NET_2_0
  375. protected
  376. #endif
  377. internal bool IsEnabled
  378. {
  379. get {
  380. #if NET_2_0
  381. WebControl wc = this;
  382. while (wc != null) {
  383. if (!wc.Enabled)
  384. return false;
  385. wc = wc.Parent as WebControl;
  386. }
  387. return true;
  388. #else
  389. return Enabled;
  390. #endif
  391. }
  392. }
  393. #if NET_4_0
  394. public static string DisabledCssClass {
  395. get;
  396. set;
  397. }
  398. [Browsable (false)]
  399. public virtual bool SupportsDisabledAttribute {
  400. get { return true; }
  401. }
  402. #endif
  403. public void ApplyStyle (Style s)
  404. {
  405. if (s != null && !s.IsEmpty)
  406. ControlStyle.CopyFrom(s);
  407. }
  408. public void CopyBaseAttributes (WebControl controlSrc)
  409. {
  410. object o;
  411. if (controlSrc == null)
  412. return;
  413. Enabled = controlSrc.Enabled;
  414. o = controlSrc.ViewState ["AccessKey"];
  415. if (o != null)
  416. ViewState ["AccessKey"] = o;
  417. o = controlSrc.ViewState ["TabIndex"];
  418. if (o != null)
  419. ViewState ["TabIndex"] = o;
  420. o = controlSrc.ViewState ["ToolTip"];
  421. if (o != null)
  422. ViewState ["ToolTip"] = o;
  423. if (controlSrc.attributes != null) {
  424. AttributeCollection attributes = Attributes;
  425. foreach (string s in controlSrc.attributes.Keys)
  426. attributes [s] = controlSrc.attributes [s];
  427. }
  428. }
  429. public void MergeStyle (Style s)
  430. {
  431. if (s != null && !s.IsEmpty)
  432. ControlStyle.MergeWith(s);
  433. }
  434. public virtual void RenderBeginTag (HtmlTextWriter writer)
  435. {
  436. AddAttributesToRender (writer);
  437. if (TagKey == HtmlTextWriterTag.Unknown)
  438. writer.RenderBeginTag (TagName);
  439. else
  440. writer.RenderBeginTag (TagKey);
  441. }
  442. public virtual void RenderEndTag (HtmlTextWriter writer)
  443. {
  444. writer.RenderEndTag ();
  445. }
  446. static char[] _script_trim_chars = {';'};
  447. internal string BuildScriptAttribute (string name, string tail)
  448. {
  449. AttributeCollection attrs = Attributes;
  450. string attr = attrs [name];
  451. if (attr == null || attr.Length == 0)
  452. return tail;
  453. if (attr [attr.Length - 1] == ';')
  454. attr = attr.TrimEnd (_script_trim_chars);
  455. attr = String.Concat (attr, ";", tail);
  456. attrs.Remove (name);
  457. return attr;
  458. }
  459. #if NET_2_0
  460. internal void AddDisplayStyleAttribute (HtmlTextWriter writer)
  461. {
  462. if (!ControlStyleCreated)
  463. return;
  464. if (!ControlStyle.BorderWidth.IsEmpty ||
  465. (ControlStyle.BorderStyle != BorderStyle.None && ControlStyle.BorderStyle != BorderStyle.NotSet) ||
  466. !ControlStyle.Height.IsEmpty ||
  467. !ControlStyle.Width.IsEmpty)
  468. writer.AddStyleAttribute (HtmlTextWriterStyle.Display, "inline-block");
  469. }
  470. #endif
  471. void RenderDisabled (HtmlTextWriter writer)
  472. {
  473. if (!IsEnabled) {
  474. #if NET_4_0
  475. if (!SupportsDisabledAttribute)
  476. ControlStyle.PrependCssClass (DisabledCssClass);
  477. else
  478. #endif
  479. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  480. }
  481. }
  482. protected virtual void AddAttributesToRender (HtmlTextWriter writer)
  483. {
  484. #if NET_4_0
  485. RenderDisabled (writer);
  486. #endif
  487. if (ID != null)
  488. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  489. #if !NET_4_0
  490. RenderDisabled (writer);
  491. #endif
  492. if (AccessKey != string.Empty)
  493. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  494. if (ToolTip != string.Empty)
  495. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  496. if (TabIndex != 0)
  497. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
  498. if (style != null && !style.IsEmpty) {
  499. #if NET_2_0
  500. //unbelievable, but see WebControlTest.RenderBeginTag_BorderWidth_xxx
  501. if (TagKey == HtmlTextWriterTag.Span)
  502. AddDisplayStyleAttribute (writer);
  503. #endif
  504. style.AddAttributesToRender(writer, this);
  505. }
  506. if (attributes != null)
  507. foreach(string s in attributes.Keys)
  508. writer.AddAttribute (s, attributes [s]);
  509. }
  510. protected virtual Style CreateControlStyle()
  511. {
  512. return new Style (ViewState);
  513. }
  514. protected override void LoadViewState (object savedState)
  515. {
  516. if (savedState == null || !(savedState is Pair)) {
  517. base.LoadViewState (null);
  518. return;
  519. }
  520. Pair pair = (Pair) savedState;
  521. base.LoadViewState (pair.First);
  522. if (ViewState [System.Web.UI.WebControls.Style.BitStateKey] != null)
  523. ControlStyle.LoadBitState ();
  524. if (pair.Second != null) {
  525. if (attribute_state == null) {
  526. attribute_state = new StateBag ();
  527. if (IsTrackingViewState)
  528. attribute_state.TrackViewState ();
  529. }
  530. attribute_state.LoadViewState (pair.Second);
  531. attributes = new AttributeCollection(attribute_state);
  532. }
  533. enabled = ViewState.GetBool ("Enabled", enabled);
  534. }
  535. #if NET_2_0
  536. protected internal
  537. #else
  538. protected
  539. #endif
  540. override void Render (HtmlTextWriter writer)
  541. {
  542. #if NET_2_0
  543. if (Adapter != null) {
  544. Adapter.Render(writer);
  545. return;
  546. }
  547. #endif
  548. RenderBeginTag (writer);
  549. RenderContents (writer);
  550. RenderEndTag (writer);
  551. }
  552. #if NET_2_0
  553. protected internal
  554. #else
  555. protected
  556. #endif
  557. virtual void RenderContents (HtmlTextWriter writer)
  558. {
  559. base.Render (writer);
  560. }
  561. protected override object SaveViewState ()
  562. {
  563. if (track_enabled_state)
  564. ViewState ["Enabled"] = enabled;
  565. object view_state;
  566. object attr_view_state = null;
  567. if (style != null)
  568. style.SaveBitState ();
  569. view_state = base.SaveViewState ();
  570. if (attribute_state != null)
  571. attr_view_state = attribute_state.SaveViewState ();
  572. if (view_state == null && attr_view_state == null)
  573. return null;
  574. return new Pair (view_state, attr_view_state);
  575. }
  576. protected override void TrackViewState()
  577. {
  578. if (style != null)
  579. style.TrackViewState ();
  580. if (attribute_state != null) {
  581. attribute_state.TrackViewState ();
  582. attribute_state.SetDirty (true);
  583. }
  584. base.TrackViewState ();
  585. }
  586. string IAttributeAccessor.GetAttribute (string key)
  587. {
  588. if (attributes != null)
  589. return attributes [key];
  590. return null;
  591. }
  592. void IAttributeAccessor.SetAttribute (string key, string value)
  593. {
  594. Attributes [key] = value;
  595. }
  596. }
  597. }