CheckBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //
  2. // System.Web.UI.WebControls.CheckBox.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Web.UI;
  29. using System.Collections.Specialized;
  30. using System.ComponentModel;
  31. namespace System.Web.UI.WebControls {
  32. [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  33. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  34. [DefaultEvent ("CheckedChanged")]
  35. [DefaultProperty ("Text")]
  36. #if NET_2_0
  37. [ControlValueProperty ("Checked", null)]
  38. #endif
  39. public class CheckBox : WebControl, IPostBackDataHandler
  40. #if NET_2_0
  41. , ICheckBoxControl
  42. #endif
  43. {
  44. string render_type;
  45. #if NET_2_0
  46. AttributeCollection inputAttributes;
  47. StateBag inputAttributesState;
  48. AttributeCollection labelAttributes;
  49. StateBag labelAttributesState;
  50. #endif
  51. public CheckBox () : base (HtmlTextWriterTag.Input)
  52. {
  53. render_type = "checkbox";
  54. }
  55. internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
  56. {
  57. this.render_type = render_type;
  58. }
  59. [DefaultValue (false)]
  60. #if NET_2_0
  61. [Themeable (false)]
  62. #endif
  63. [WebSysDescription ("")]
  64. [WebCategory ("Behavior")]
  65. public virtual bool AutoPostBack
  66. {
  67. get {
  68. return (ViewState.GetBool ("AutoPostBack",
  69. false));
  70. }
  71. set {
  72. ViewState["AutoPostBack"] = value;
  73. }
  74. }
  75. #if NET_2_0
  76. [DefaultValue (false)]
  77. [Themeable (false)]
  78. [MonoTODO]
  79. [WebSysDescription ("")]
  80. [WebCategory ("Behavior")]
  81. public virtual bool CausesValidation
  82. {
  83. get { return ViewState.GetBool ("CausesValidation", false); }
  84. set { ViewState ["CausesValidation"] = value; }
  85. }
  86. #endif
  87. [DefaultValue (false)]
  88. #if NET_2_0
  89. [Bindable (true, BindingDirection.TwoWay)]
  90. [Themeable (false)]
  91. #else
  92. [Bindable (true)]
  93. #endif
  94. [WebSysDescription ("")]
  95. [WebCategory ("Behavior")]
  96. public virtual bool Checked
  97. {
  98. get {
  99. return (ViewState.GetBool ("Checked", false));
  100. }
  101. set {
  102. ViewState["Checked"] = value;
  103. }
  104. }
  105. #if NET_2_0
  106. [Browsable (false)]
  107. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  108. public AttributeCollection InputAttributes
  109. {
  110. get {
  111. if (inputAttributes == null) {
  112. if (inputAttributesState == null) {
  113. inputAttributesState = new StateBag (true);
  114. if (IsTrackingViewState)
  115. inputAttributesState.TrackViewState();
  116. }
  117. inputAttributes = new AttributeCollection (inputAttributesState);
  118. }
  119. return inputAttributes;
  120. }
  121. }
  122. [Browsable (false)]
  123. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  124. public AttributeCollection LabelAttributes
  125. {
  126. get {
  127. if (labelAttributes == null) {
  128. if (labelAttributesState == null) {
  129. labelAttributesState = new StateBag (true);
  130. if (IsTrackingViewState)
  131. labelAttributesState.TrackViewState();
  132. }
  133. labelAttributes = new AttributeCollection (labelAttributesState);
  134. }
  135. return labelAttributes;
  136. }
  137. }
  138. #endif
  139. [DefaultValue ("")]
  140. [Bindable (true)]
  141. #if NET_2_0
  142. [Localizable (true)]
  143. #endif
  144. [WebSysDescription ("")]
  145. [WebCategory ("Appearance")]
  146. public virtual string Text
  147. {
  148. get {
  149. return (ViewState.GetString ("Text",
  150. String.Empty));
  151. }
  152. set {
  153. ViewState["Text"] = value;
  154. }
  155. }
  156. [DefaultValue (TextAlign.Right)]
  157. #if ONLY_1_1
  158. [Bindable (true)]
  159. #endif
  160. [WebSysDescription ("")]
  161. [WebCategory ("Appearance")]
  162. public virtual TextAlign TextAlign
  163. {
  164. get {
  165. object o = ViewState["TextAlign"];
  166. if (o == null) {
  167. return (TextAlign.Right);
  168. } else {
  169. return ((TextAlign)o);
  170. }
  171. }
  172. set {
  173. if (value != TextAlign.Left &&
  174. value != TextAlign.Right) {
  175. throw new ArgumentOutOfRangeException ("value");
  176. }
  177. ViewState["TextAlign"] = value;
  178. }
  179. }
  180. #if NET_2_0
  181. [Themeable (false)]
  182. [DefaultValue ("")]
  183. [WebSysDescription ("")]
  184. [WebCategoryAttribute ("Behavior")]
  185. public string ValidationGroup
  186. {
  187. get { return ViewState.GetString ("ValidationGroup", String.Empty); }
  188. set { ViewState["ValidationGroup"] = value; }
  189. }
  190. #endif
  191. private static readonly object EventCheckedChanged = new object ();
  192. [WebSysDescription ("")]
  193. [WebCategory ("Action")]
  194. public event EventHandler CheckedChanged
  195. {
  196. add {
  197. Events.AddHandler (EventCheckedChanged, value);
  198. }
  199. remove {
  200. Events.RemoveHandler (EventCheckedChanged, value);
  201. }
  202. }
  203. protected virtual void OnCheckedChanged (EventArgs e)
  204. {
  205. EventHandler handler = (EventHandler)Events[EventCheckedChanged];
  206. if (handler != null) {
  207. handler (this, e);
  208. }
  209. }
  210. internal virtual string NameAttribute
  211. {
  212. get {
  213. return (this.UniqueID);
  214. }
  215. }
  216. protected override void AddAttributesToRender (HtmlTextWriter w)
  217. {
  218. if (Page != null)
  219. Page.VerifyRenderingInServerForm (this);
  220. /* This is a nasty kludge to avoid rendering
  221. * "style" and the ControlStyle in checkboxes
  222. * (we use a surrounding <span> instead), but
  223. * still pass the unit test that shows the
  224. * style being rendered in multiple calls to
  225. * Render () (which means we can't just delete
  226. * Attributes["style"] or ControlStyle.Reset()
  227. * in Render ())
  228. */
  229. string css_style = Attributes ["style"];
  230. if (css_style != null) {
  231. Attributes.Remove ("style");
  232. }
  233. Style style = new Style ();
  234. if (ControlStyleCreated) {
  235. style.CopyFrom (ControlStyle);
  236. ControlStyle.Reset ();
  237. }
  238. base.AddAttributesToRender (w);
  239. if (css_style != null) {
  240. Attributes ["style"] = css_style;
  241. }
  242. if (!style.IsEmpty) {
  243. ApplyStyle (style);
  244. }
  245. InternalAddAttributesToRender (w);
  246. w.AddAttribute (HtmlTextWriterAttribute.Type,
  247. render_type);
  248. w.AddAttribute (HtmlTextWriterAttribute.Name,
  249. NameAttribute);
  250. if (AutoPostBack) {
  251. w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  252. }
  253. if (Checked) {
  254. w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  255. }
  256. }
  257. #if NET_2_0
  258. protected override void LoadViewState (object savedState)
  259. {
  260. if (savedState == null) {
  261. base.LoadViewState (null);
  262. return;
  263. }
  264. Triplet saved = (Triplet) savedState;
  265. base.LoadViewState (saved.First);
  266. if (saved.Second != null) {
  267. if (inputAttributesState == null) {
  268. inputAttributesState = new StateBag(true);
  269. inputAttributesState.TrackViewState ();
  270. }
  271. inputAttributesState.LoadViewState (saved.Second);
  272. }
  273. if (saved.Third != null) {
  274. if (labelAttributesState == null) {
  275. labelAttributesState = new StateBag(true);
  276. labelAttributesState.TrackViewState ();
  277. }
  278. labelAttributesState.LoadViewState (saved.Third);
  279. }
  280. }
  281. protected override object SaveViewState ()
  282. {
  283. object baseView = base.SaveViewState ();
  284. object inputAttrView = null;
  285. object labelAttrView = null;
  286. if (inputAttributesState != null)
  287. inputAttrView = inputAttributesState.SaveViewState ();
  288. if (labelAttributesState != null)
  289. labelAttrView = labelAttributesState.SaveViewState ();
  290. if (baseView == null && inputAttrView == null && labelAttrView == null)
  291. return null;
  292. return new Triplet (baseView, inputAttrView, labelAttrView);
  293. }
  294. protected override void TrackViewState ()
  295. {
  296. base.TrackViewState();
  297. if (inputAttributesState != null)
  298. inputAttributesState.TrackViewState ();
  299. if (labelAttributesState != null)
  300. labelAttributesState.TrackViewState ();
  301. }
  302. #endif
  303. #if NET_2_0
  304. protected internal
  305. #else
  306. protected
  307. #endif
  308. override void OnPreRender (EventArgs e)
  309. {
  310. base.OnPreRender (e);
  311. if (Page != null) {
  312. Page.RegisterRequiresPostBack (this);
  313. }
  314. }
  315. void RenderLabel (HtmlTextWriter w)
  316. {
  317. if (Text.Length > 0) {
  318. #if NET_2_0
  319. if (labelAttributes != null)
  320. labelAttributes.AddAttributes (w);
  321. #endif
  322. w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  323. w.RenderBeginTag (HtmlTextWriterTag.Label);
  324. w.Write (this.Text);
  325. w.RenderEndTag ();
  326. }
  327. }
  328. #if NET_2_0
  329. protected internal
  330. #else
  331. protected
  332. #endif
  333. override void Render (HtmlTextWriter w)
  334. {
  335. bool control_style = false;
  336. /* Need to apply the styles around the text
  337. * label too
  338. */
  339. if (ControlStyleCreated) {
  340. ControlStyle.AddAttributesToRender (w, this);
  341. w.RenderBeginTag (HtmlTextWriterTag.Span);
  342. control_style = true;
  343. } else if (Attributes ["style"] != null) {
  344. /* TODO: check if this or the style
  345. * has precendence, or if they should
  346. * be merged (if I can figure out how
  347. * to turn a CssStyleCollection into a
  348. * Style)
  349. */
  350. CssStyleCollection style = Attributes.CssStyle;
  351. w.AddAttribute (HtmlTextWriterAttribute.Style,
  352. style.BagToString ());
  353. w.RenderBeginTag (HtmlTextWriterTag.Span);
  354. control_style = true;
  355. }
  356. if (TextAlign == TextAlign.Left) {
  357. RenderLabel (w);
  358. base.Render (w);
  359. } else {
  360. base.Render (w);
  361. RenderLabel (w);
  362. }
  363. if (control_style) {
  364. w.RenderEndTag ();
  365. }
  366. }
  367. #if NET_2_0
  368. protected virtual
  369. #endif
  370. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  371. {
  372. string postedValue = postCollection[postDataKey];
  373. bool postedBool = ((postedValue != null) &&
  374. (postedValue.Length > 0));
  375. if (Checked != postedBool) {
  376. Checked = postedBool;
  377. return (true);
  378. }
  379. return (false);
  380. }
  381. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  382. {
  383. return LoadPostData (postDataKey, postCollection);
  384. }
  385. #if NET_2_0
  386. protected virtual void RaisePostDataChangedEvent ()
  387. {
  388. if (CausesValidation)
  389. Page.Validate (ValidationGroup);
  390. OnCheckedChanged (EventArgs.Empty);
  391. }
  392. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  393. {
  394. RaisePostDataChangedEvent ();
  395. }
  396. #else
  397. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  398. {
  399. OnCheckedChanged (EventArgs.Empty);
  400. }
  401. #endif
  402. internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
  403. {
  404. }
  405. }
  406. }