WebControl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. [CssClassProperty]
  196. [DefaultValue("")]
  197. [WebSysDescription ("")]
  198. [WebCategory ("Appearance")]
  199. public virtual string CssClass {
  200. get {
  201. if (style == null)
  202. return string.Empty;
  203. return style.CssClass;
  204. }
  205. set {
  206. ControlStyle.CssClass = value;
  207. }
  208. }
  209. [Bindable(true)]
  210. [DefaultValue(true)]
  211. #if NET_2_0
  212. [Themeable (false)]
  213. #endif
  214. public virtual bool Enabled {
  215. get {
  216. return enabled;
  217. }
  218. set {
  219. if (enabled != value) {
  220. if (IsTrackingViewState)
  221. track_enabled_state = true;
  222. enabled = value;
  223. }
  224. }
  225. }
  226. #if NET_2_0
  227. [Browsable (true)]
  228. public virtual new bool EnableTheming
  229. {
  230. get { return base.EnableTheming; }
  231. set { base.EnableTheming = value; }
  232. }
  233. #endif
  234. #if ONLY_1_1
  235. [DefaultValue(null)]
  236. #endif
  237. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  238. [NotifyParentProperty(true)]
  239. [WebSysDescription ("")]
  240. [WebCategory ("Appearance")]
  241. public virtual FontInfo Font {
  242. get {
  243. // Oddly enough, it looks like we have to let it create the style
  244. // since we can't create a FontInfo without a style owner
  245. return ControlStyle.Font;
  246. }
  247. }
  248. #if ONLY_1_1
  249. [Bindable(true)]
  250. #endif
  251. [DefaultValue(typeof (Color), "")]
  252. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  253. [WebSysDescription ("")]
  254. [WebCategory ("Appearance")]
  255. public virtual Color ForeColor {
  256. get {
  257. if (style == null)
  258. return Color.Empty;
  259. return style.ForeColor;
  260. }
  261. set {
  262. ControlStyle.ForeColor = value;
  263. }
  264. }
  265. [Browsable (false)]
  266. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  267. #if NET_2_0
  268. public
  269. #else
  270. internal
  271. #endif
  272. bool HasAttributes
  273. {
  274. get {
  275. return (attributes != null && attributes.Count > 0);
  276. }
  277. }
  278. #if ONLY_1_1
  279. [Bindable(true)]
  280. #endif
  281. [DefaultValue(typeof (Unit), "")]
  282. [WebSysDescription ("")]
  283. [WebCategory ("Layout")]
  284. public virtual Unit Height {
  285. get {
  286. if (style == null)
  287. return Unit.Empty;
  288. return style.Height;
  289. }
  290. set {
  291. ControlStyle.Height = value;
  292. }
  293. }
  294. #if NET_2_0
  295. [Browsable (true)]
  296. public virtual new string SkinID
  297. {
  298. get { return base.SkinID; }
  299. set { base.SkinID = value; }
  300. }
  301. #endif
  302. [Browsable(false)]
  303. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  304. [WebSysDescription ("")]
  305. [WebCategory ("Style")]
  306. public CssStyleCollection Style {
  307. get {
  308. return Attributes.CssStyle;
  309. }
  310. }
  311. [DefaultValue((short)0)]
  312. [WebSysDescription ("")]
  313. [WebCategory ("Behavior")]
  314. public virtual short TabIndex {
  315. get {
  316. return ViewState.GetShort ("TabIndex", 0);
  317. }
  318. set {
  319. ViewState ["TabIndex"] = value;
  320. }
  321. }
  322. #if ONLY_1_1
  323. [Bindable(true)]
  324. #endif
  325. [DefaultValue("")]
  326. #if NET_2_0
  327. [Localizable (true)]
  328. #endif
  329. [WebSysDescription ("")]
  330. [WebCategory ("Behavior")]
  331. public virtual string ToolTip {
  332. get {
  333. return ViewState.GetString ("ToolTip", string.Empty);
  334. }
  335. set {
  336. ViewState ["ToolTip"] = value;
  337. }
  338. }
  339. #if ONLY_1_1
  340. [Bindable(true)]
  341. #endif
  342. [DefaultValue(typeof (Unit), "")]
  343. [WebSysDescription ("")]
  344. [WebCategory ("Layout")]
  345. public virtual Unit Width {
  346. get {
  347. if (style == null)
  348. return Unit.Empty;
  349. return style.Width;
  350. }
  351. set {
  352. ControlStyle.Width = value;
  353. }
  354. }
  355. [Browsable(false)]
  356. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  357. protected virtual HtmlTextWriterTag TagKey {
  358. get {
  359. return tag;
  360. }
  361. }
  362. [Browsable(false)]
  363. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  364. protected virtual string TagName {
  365. get {
  366. // do this here to avoid potentially costly lookups on every control
  367. if (tag_name == null)
  368. tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
  369. return tag_name;
  370. }
  371. }
  372. #if NET_2_0
  373. protected
  374. #endif
  375. internal bool IsEnabled
  376. {
  377. get {
  378. #if NET_2_0
  379. WebControl wc = this;
  380. while (wc != null) {
  381. if (!wc.Enabled)
  382. return false;
  383. wc = wc.Parent as WebControl;
  384. }
  385. return true;
  386. #else
  387. return Enabled;
  388. #endif
  389. }
  390. }
  391. #if NET_4_0
  392. public static string DisabledCssClass {
  393. get;
  394. set;
  395. }
  396. [Browsable (false)]
  397. public virtual bool SupportsDisabledAttribute {
  398. get { return true; }
  399. }
  400. #endif
  401. public void ApplyStyle (Style s)
  402. {
  403. if (s != null && !s.IsEmpty)
  404. ControlStyle.CopyFrom(s);
  405. }
  406. public void CopyBaseAttributes (WebControl controlSrc)
  407. {
  408. object o;
  409. if (controlSrc == null)
  410. return;
  411. Enabled = controlSrc.Enabled;
  412. o = controlSrc.ViewState ["AccessKey"];
  413. if (o != null)
  414. ViewState ["AccessKey"] = o;
  415. o = controlSrc.ViewState ["TabIndex"];
  416. if (o != null)
  417. ViewState ["TabIndex"] = o;
  418. o = controlSrc.ViewState ["ToolTip"];
  419. if (o != null)
  420. ViewState ["ToolTip"] = o;
  421. if (controlSrc.attributes != null) {
  422. AttributeCollection attributes = Attributes;
  423. foreach (string s in controlSrc.attributes.Keys)
  424. attributes [s] = controlSrc.attributes [s];
  425. }
  426. }
  427. public void MergeStyle (Style s)
  428. {
  429. if (s != null && !s.IsEmpty)
  430. ControlStyle.MergeWith(s);
  431. }
  432. public virtual void RenderBeginTag (HtmlTextWriter writer)
  433. {
  434. AddAttributesToRender (writer);
  435. if (TagKey == HtmlTextWriterTag.Unknown)
  436. writer.RenderBeginTag (TagName);
  437. else
  438. writer.RenderBeginTag (TagKey);
  439. }
  440. public virtual void RenderEndTag (HtmlTextWriter writer)
  441. {
  442. writer.RenderEndTag ();
  443. }
  444. static char[] _script_trim_chars = {';'};
  445. internal string BuildScriptAttribute (string name, string tail)
  446. {
  447. AttributeCollection attrs = Attributes;
  448. string attr = attrs [name];
  449. if (attr == null || attr.Length == 0)
  450. return tail;
  451. if (attr [attr.Length - 1] == ';')
  452. attr = attr.TrimEnd (_script_trim_chars);
  453. attr = String.Concat (attr, ";", tail);
  454. attrs.Remove (name);
  455. return attr;
  456. }
  457. #if NET_2_0
  458. internal void AddDisplayStyleAttribute (HtmlTextWriter writer)
  459. {
  460. if (!ControlStyleCreated)
  461. return;
  462. if (!ControlStyle.BorderWidth.IsEmpty ||
  463. (ControlStyle.BorderStyle != BorderStyle.None && ControlStyle.BorderStyle != BorderStyle.NotSet) ||
  464. !ControlStyle.Height.IsEmpty ||
  465. !ControlStyle.Width.IsEmpty)
  466. writer.AddStyleAttribute (HtmlTextWriterStyle.Display, "inline-block");
  467. }
  468. #endif
  469. void RenderDisabled (HtmlTextWriter writer)
  470. {
  471. if (!IsEnabled) {
  472. #if NET_4_0
  473. if (!SupportsDisabledAttribute)
  474. ControlStyle.PrependCssClass (DisabledCssClass);
  475. else
  476. #endif
  477. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  478. }
  479. }
  480. protected virtual void AddAttributesToRender (HtmlTextWriter writer)
  481. {
  482. #if NET_4_0
  483. RenderDisabled (writer);
  484. #endif
  485. if (ID != null)
  486. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  487. #if !NET_4_0
  488. RenderDisabled (writer);
  489. #endif
  490. if (AccessKey != string.Empty)
  491. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  492. if (ToolTip != string.Empty)
  493. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  494. if (TabIndex != 0)
  495. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
  496. if (style != null && !style.IsEmpty) {
  497. #if NET_2_0
  498. //unbelievable, but see WebControlTest.RenderBeginTag_BorderWidth_xxx
  499. if (TagKey == HtmlTextWriterTag.Span)
  500. AddDisplayStyleAttribute (writer);
  501. #endif
  502. style.AddAttributesToRender(writer, this);
  503. }
  504. if (attributes != null)
  505. foreach(string s in attributes.Keys)
  506. writer.AddAttribute (s, attributes [s]);
  507. }
  508. protected virtual Style CreateControlStyle()
  509. {
  510. return new Style (ViewState);
  511. }
  512. protected override void LoadViewState (object savedState)
  513. {
  514. if (savedState == null || !(savedState is Pair)) {
  515. base.LoadViewState (null);
  516. return;
  517. }
  518. Pair pair = (Pair) savedState;
  519. base.LoadViewState (pair.First);
  520. if (ViewState [System.Web.UI.WebControls.Style.BitStateKey] != null)
  521. ControlStyle.LoadBitState ();
  522. if (pair.Second != null) {
  523. if (attribute_state == null) {
  524. attribute_state = new StateBag ();
  525. if (IsTrackingViewState)
  526. attribute_state.TrackViewState ();
  527. }
  528. attribute_state.LoadViewState (pair.Second);
  529. attributes = new AttributeCollection(attribute_state);
  530. }
  531. enabled = ViewState.GetBool ("Enabled", enabled);
  532. }
  533. #if NET_2_0
  534. protected internal
  535. #else
  536. protected
  537. #endif
  538. override void Render (HtmlTextWriter writer)
  539. {
  540. #if NET_2_0
  541. if (Adapter != null) {
  542. Adapter.Render(writer);
  543. return;
  544. }
  545. #endif
  546. RenderBeginTag (writer);
  547. RenderContents (writer);
  548. RenderEndTag (writer);
  549. }
  550. #if NET_2_0
  551. protected internal
  552. #else
  553. protected
  554. #endif
  555. virtual void RenderContents (HtmlTextWriter writer)
  556. {
  557. base.Render (writer);
  558. }
  559. protected override object SaveViewState ()
  560. {
  561. if (track_enabled_state)
  562. ViewState ["Enabled"] = enabled;
  563. object view_state;
  564. object attr_view_state = null;
  565. if (style != null)
  566. style.SaveBitState ();
  567. view_state = base.SaveViewState ();
  568. if (attribute_state != null)
  569. attr_view_state = attribute_state.SaveViewState ();
  570. if (view_state == null && attr_view_state == null)
  571. return null;
  572. return new Pair (view_state, attr_view_state);
  573. }
  574. protected override void TrackViewState()
  575. {
  576. if (style != null)
  577. style.TrackViewState ();
  578. if (attribute_state != null) {
  579. attribute_state.TrackViewState ();
  580. attribute_state.SetDirty (true);
  581. }
  582. base.TrackViewState ();
  583. }
  584. string IAttributeAccessor.GetAttribute (string key)
  585. {
  586. if (attributes != null)
  587. return attributes [key];
  588. return null;
  589. }
  590. void IAttributeAccessor.SetAttribute (string key, string value)
  591. {
  592. Attributes [key] = value;
  593. }
  594. }
  595. }