CheckBox.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. public CheckBox () : base (HtmlTextWriterTag.Input)
  46. {
  47. render_type = "checkbox";
  48. }
  49. internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
  50. {
  51. this.render_type = render_type;
  52. }
  53. [DefaultValue (false)]
  54. #if NET_2_0
  55. [Themeable (false)]
  56. #endif
  57. public virtual bool AutoPostBack
  58. {
  59. get {
  60. return (ViewState.GetBool ("AutoPostBack",
  61. false));
  62. }
  63. set {
  64. ViewState["AutoPostBack"] = value;
  65. }
  66. }
  67. #if NET_2_0
  68. [DefaultValue (false)]
  69. [Themeable (false)]
  70. [MonoTODO]
  71. public virtual bool CausesValidation
  72. {
  73. get {
  74. throw new NotImplementedException ();
  75. }
  76. set {
  77. throw new NotImplementedException ();
  78. }
  79. }
  80. #endif
  81. [DefaultValue (false)]
  82. #if NET_2_0
  83. [Bindable (true, BindingDirection.TwoWay)]
  84. [Themeable (false)]
  85. #else
  86. [Bindable (true)]
  87. #endif
  88. public virtual bool Checked
  89. {
  90. get {
  91. return (ViewState.GetBool ("Checked", false));
  92. }
  93. set {
  94. ViewState["Checked"] = value;
  95. }
  96. }
  97. #if NET_2_0
  98. [Browsable (false)]
  99. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  100. [MonoTODO]
  101. public AttributeCollection InputAttributes
  102. {
  103. get {
  104. throw new NotImplementedException ();
  105. }
  106. set {
  107. throw new NotImplementedException ();
  108. }
  109. }
  110. [Browsable (false)]
  111. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  112. [MonoTODO]
  113. public AttributeCollection LabelAttributes
  114. {
  115. get {
  116. throw new NotImplementedException ();
  117. }
  118. set {
  119. throw new NotImplementedException ();
  120. }
  121. }
  122. #endif
  123. [DefaultValue ("")]
  124. [Bindable (true)]
  125. #if NET_2_0
  126. [Localizable (true)]
  127. #endif
  128. public virtual string Text
  129. {
  130. get {
  131. return (ViewState.GetString ("Text",
  132. String.Empty));
  133. }
  134. set {
  135. ViewState["Text"] = value;
  136. }
  137. }
  138. [DefaultValue (TextAlign.Right)]
  139. #if ONLY_1_1
  140. [Bindable (true)]
  141. #endif
  142. public virtual TextAlign TextAlign
  143. {
  144. get {
  145. object o = ViewState["TextAlign"];
  146. if (o == null) {
  147. return (TextAlign.Right);
  148. } else {
  149. return ((TextAlign)o);
  150. }
  151. }
  152. set {
  153. if (value != TextAlign.Left &&
  154. value != TextAlign.Right) {
  155. throw new ArgumentOutOfRangeException ("value");
  156. }
  157. ViewState["TextAlign"] = value;
  158. }
  159. }
  160. #if NET_2_0
  161. [Themeable (false)]
  162. [DefaultValue ("")]
  163. [MonoTODO]
  164. public string ValidationGroup
  165. {
  166. get {
  167. throw new NotImplementedException ();
  168. }
  169. set {
  170. throw new NotImplementedException ();
  171. }
  172. }
  173. #endif
  174. private static readonly object EventCheckedChanged = new object ();
  175. public event EventHandler CheckedChanged
  176. {
  177. add {
  178. Events.AddHandler (EventCheckedChanged, value);
  179. }
  180. remove {
  181. Events.RemoveHandler (EventCheckedChanged, value);
  182. }
  183. }
  184. protected virtual void OnCheckedChanged (EventArgs e)
  185. {
  186. EventHandler handler = (EventHandler)Events[EventCheckedChanged];
  187. if (handler != null) {
  188. handler (this, e);
  189. }
  190. }
  191. internal virtual string NameAttribute
  192. {
  193. get {
  194. return (this.UniqueID);
  195. }
  196. }
  197. protected override void AddAttributesToRender (HtmlTextWriter w)
  198. {
  199. if (Page != null)
  200. Page.VerifyRenderingInServerForm (this);
  201. /* This is a nasty kludge to avoid rendering
  202. * "style" and the ControlStyle in checkboxes
  203. * (we use a surrounding <span> instead), but
  204. * still pass the unit test that shows the
  205. * style being rendered in multiple calls to
  206. * Render () (which means we can't just delete
  207. * Attributes["style"] or ControlStyle.Reset()
  208. * in Render ())
  209. */
  210. string css_style = Attributes ["style"];
  211. if (css_style != null) {
  212. Attributes.Remove ("style");
  213. }
  214. Style style = new Style ();
  215. if (ControlStyleCreated) {
  216. style.CopyFrom (ControlStyle);
  217. ControlStyle.Reset ();
  218. }
  219. base.AddAttributesToRender (w);
  220. if (css_style != null) {
  221. Attributes ["style"] = css_style;
  222. }
  223. if (!style.IsEmpty) {
  224. ApplyStyle (style);
  225. }
  226. InternalAddAttributesToRender (w);
  227. w.AddAttribute (HtmlTextWriterAttribute.Type,
  228. render_type);
  229. w.AddAttribute (HtmlTextWriterAttribute.Name,
  230. NameAttribute);
  231. if (AutoPostBack) {
  232. w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.GetPostBackClientHyperlink (this, ""));
  233. }
  234. if (Checked) {
  235. w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  236. }
  237. }
  238. #if NET_2_0
  239. [MonoTODO]
  240. protected override void LoadViewState (object savedState)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. [MonoTODO]
  245. protected override object SaveViewState ()
  246. {
  247. throw new NotImplementedException ();
  248. }
  249. [MonoTODO]
  250. protected override void TrackViewState ()
  251. {
  252. throw new NotImplementedException ();
  253. }
  254. #endif
  255. #if NET_2_0
  256. protected internal
  257. #else
  258. protected
  259. #endif
  260. override void OnPreRender (EventArgs e)
  261. {
  262. base.OnPreRender (e);
  263. if (Page != null) {
  264. Page.RegisterRequiresPostBack (this);
  265. }
  266. }
  267. void RenderLabel (HtmlTextWriter w)
  268. {
  269. if (Text.Length > 0) {
  270. w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  271. w.RenderBeginTag (HtmlTextWriterTag.Label);
  272. w.Write (this.Text);
  273. w.RenderEndTag ();
  274. }
  275. }
  276. #if NET_2_0
  277. protected internal
  278. #else
  279. protected
  280. #endif
  281. override void Render (HtmlTextWriter w)
  282. {
  283. bool control_style = false;
  284. /* Need to apply the styles around the text
  285. * label too
  286. */
  287. if (ControlStyleCreated) {
  288. ControlStyle.AddAttributesToRender (w, this);
  289. w.RenderBeginTag (HtmlTextWriterTag.Span);
  290. control_style = true;
  291. } else if (Attributes ["style"] != null) {
  292. /* TODO: check if this or the style
  293. * has precendence, or if they should
  294. * be merged (if I can figure out how
  295. * to turn a CssStyleCollection into a
  296. * Style)
  297. */
  298. CssStyleCollection style = Attributes.CssStyle;
  299. w.AddAttribute (HtmlTextWriterAttribute.Style,
  300. style.BagToString ());
  301. w.RenderBeginTag (HtmlTextWriterTag.Span);
  302. control_style = true;
  303. }
  304. if (TextAlign == TextAlign.Left) {
  305. RenderLabel (w);
  306. base.Render (w);
  307. } else {
  308. base.Render (w);
  309. RenderLabel (w);
  310. }
  311. if (control_style) {
  312. w.RenderEndTag ();
  313. }
  314. }
  315. #if NET_2_0
  316. [MonoTODO]
  317. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. [MonoTODO]
  322. protected virtual void RaisePostDataChangedEvent ()
  323. {
  324. throw new NotImplementedException ();
  325. }
  326. #endif
  327. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  328. {
  329. string postedValue = postCollection[postDataKey];
  330. bool postedBool = ((postedValue != null) &&
  331. (postedValue.Length > 0));
  332. if (Checked != postedBool) {
  333. Checked = postedBool;
  334. return (true);
  335. }
  336. return (false);
  337. }
  338. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  339. {
  340. OnCheckedChanged (EventArgs.Empty);
  341. }
  342. internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
  343. {
  344. }
  345. }
  346. }