CheckBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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.Collections;
  29. using System.Collections.Specialized;
  30. using System.ComponentModel;
  31. using System.Globalization;
  32. using System.Security.Permissions;
  33. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  40. [DefaultEvent ("CheckedChanged")]
  41. [DefaultProperty ("Text")]
  42. #if NET_2_0
  43. [ControlValueProperty ("Checked", null)]
  44. [SupportsEventValidation]
  45. #endif
  46. public class CheckBox : WebControl, IPostBackDataHandler
  47. #if NET_2_0
  48. , ICheckBoxControl
  49. #endif
  50. {
  51. string render_type;
  52. AttributeCollection common_attrs;
  53. #if NET_2_0
  54. AttributeCollection inputAttributes;
  55. StateBag inputAttributesState;
  56. AttributeCollection labelAttributes;
  57. StateBag labelAttributesState;
  58. #endif
  59. public CheckBox () : base (HtmlTextWriterTag.Input)
  60. {
  61. render_type = "checkbox";
  62. }
  63. internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
  64. {
  65. this.render_type = render_type;
  66. }
  67. [DefaultValue (false)]
  68. #if NET_2_0
  69. [Themeable (false)]
  70. #endif
  71. [WebSysDescription ("")]
  72. [WebCategory ("Behavior")]
  73. public virtual bool AutoPostBack
  74. {
  75. get {
  76. return (ViewState.GetBool ("AutoPostBack",
  77. false));
  78. }
  79. set {
  80. ViewState["AutoPostBack"] = value;
  81. }
  82. }
  83. #if NET_2_0
  84. [DefaultValue (false)]
  85. [Themeable (false)]
  86. [WebSysDescription ("")]
  87. [WebCategory ("Behavior")]
  88. public virtual bool CausesValidation
  89. {
  90. get { return ViewState.GetBool ("CausesValidation", false); }
  91. set { ViewState ["CausesValidation"] = value; }
  92. }
  93. #endif
  94. [DefaultValue (false)]
  95. #if NET_2_0
  96. [Bindable (true, BindingDirection.TwoWay)]
  97. [Themeable (false)]
  98. #else
  99. [Bindable (true)]
  100. #endif
  101. [WebSysDescription ("")]
  102. [WebCategory ("Behavior")]
  103. public virtual bool Checked
  104. {
  105. get {
  106. return (ViewState.GetBool ("Checked", false));
  107. }
  108. set {
  109. ViewState["Checked"] = value;
  110. }
  111. }
  112. #if NET_2_0
  113. [Browsable (false)]
  114. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  115. public AttributeCollection InputAttributes
  116. {
  117. get {
  118. if (inputAttributes == null) {
  119. if (inputAttributesState == null) {
  120. inputAttributesState = new StateBag (true);
  121. if (IsTrackingViewState)
  122. inputAttributesState.TrackViewState();
  123. }
  124. inputAttributes = new AttributeCollection (inputAttributesState);
  125. }
  126. return inputAttributes;
  127. }
  128. }
  129. [Browsable (false)]
  130. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  131. public AttributeCollection LabelAttributes
  132. {
  133. get {
  134. if (labelAttributes == null) {
  135. if (labelAttributesState == null) {
  136. labelAttributesState = new StateBag (true);
  137. if (IsTrackingViewState)
  138. labelAttributesState.TrackViewState();
  139. }
  140. labelAttributes = new AttributeCollection (labelAttributesState);
  141. }
  142. return labelAttributes;
  143. }
  144. }
  145. #endif
  146. [DefaultValue ("")]
  147. [Bindable (true)]
  148. #if NET_2_0
  149. [Localizable (true)]
  150. #endif
  151. [WebSysDescription ("")]
  152. [WebCategory ("Appearance")]
  153. public virtual string Text
  154. {
  155. get {
  156. return (ViewState.GetString ("Text",
  157. String.Empty));
  158. }
  159. set {
  160. ViewState["Text"] = value;
  161. }
  162. }
  163. [DefaultValue (TextAlign.Right)]
  164. #if ONLY_1_1
  165. [Bindable (true)]
  166. #endif
  167. [WebSysDescription ("")]
  168. [WebCategory ("Appearance")]
  169. public virtual TextAlign TextAlign
  170. {
  171. get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
  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 virtual 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. #if NET_2_0
  217. protected override void LoadViewState (object savedState)
  218. {
  219. if (savedState == null) {
  220. base.LoadViewState (null);
  221. return;
  222. }
  223. Triplet saved = (Triplet) savedState;
  224. base.LoadViewState (saved.First);
  225. if (saved.Second != null) {
  226. if (inputAttributesState == null) {
  227. inputAttributesState = new StateBag(true);
  228. inputAttributesState.TrackViewState ();
  229. }
  230. inputAttributesState.LoadViewState (saved.Second);
  231. }
  232. if (saved.Third != null) {
  233. if (labelAttributesState == null) {
  234. labelAttributesState = new StateBag(true);
  235. labelAttributesState.TrackViewState ();
  236. }
  237. labelAttributesState.LoadViewState (saved.Third);
  238. }
  239. }
  240. protected override object SaveViewState ()
  241. {
  242. object baseView = base.SaveViewState ();
  243. object inputAttrView = null;
  244. object labelAttrView = null;
  245. if (inputAttributesState != null)
  246. inputAttrView = inputAttributesState.SaveViewState ();
  247. if (labelAttributesState != null)
  248. labelAttrView = labelAttributesState.SaveViewState ();
  249. if (baseView == null && inputAttrView == null && labelAttrView == null)
  250. return null;
  251. return new Triplet (baseView, inputAttrView, labelAttrView);
  252. }
  253. protected override void TrackViewState ()
  254. {
  255. base.TrackViewState();
  256. if (inputAttributesState != null)
  257. inputAttributesState.TrackViewState ();
  258. if (labelAttributesState != null)
  259. labelAttributesState.TrackViewState ();
  260. }
  261. #endif
  262. #if NET_2_0
  263. protected internal
  264. #else
  265. protected
  266. #endif
  267. override void OnPreRender (EventArgs e)
  268. {
  269. base.OnPreRender (e);
  270. if (Page != null && Enabled) {
  271. Page.RegisterRequiresPostBack (this);
  272. }
  273. #if NET_2_0
  274. if (Page != null && Enabled)
  275. Page.RegisterEnabledControl (this);
  276. #endif
  277. }
  278. static bool IsInputOrCommonAttr (string attname)
  279. {
  280. attname = attname.ToUpper (CultureInfo.InvariantCulture);
  281. switch (attname) {
  282. case "VALUE":
  283. case "CHECKED":
  284. case "SIZE":
  285. case "MAXLENGTH":
  286. case "SRC":
  287. case "ALT":
  288. case "USEMAP":
  289. case "DISABLED":
  290. case "READONLY":
  291. case "ACCEPT":
  292. case "ACCESSKEY":
  293. case "TABINDEX":
  294. case "ONFOCUS":
  295. case "ONBLUR":
  296. case "ONSELECT":
  297. case "ONCHANGE":
  298. case "ONCLICK":
  299. case "ONDBLCLICK":
  300. case "ONMOUSEDOWN":
  301. case "ONMOUSEUP":
  302. case "ONMOUSEOVER":
  303. case "ONMOUSEMOVE":
  304. case "ONMOUSEOUT":
  305. case "ONKEYPRESS":
  306. case "ONKEYDOWN":
  307. case "ONKEYUP":
  308. return true;
  309. default:
  310. return false;
  311. }
  312. }
  313. bool AddAttributesForSpan (HtmlTextWriter writer)
  314. {
  315. ICollection k = Attributes.Keys;
  316. string [] keys = new string [k.Count];
  317. k.CopyTo (keys, 0);
  318. foreach (string key in keys) {
  319. if (!IsInputOrCommonAttr (key))
  320. continue;
  321. if (common_attrs == null)
  322. common_attrs = new AttributeCollection (new StateBag ());
  323. common_attrs [key] = Attributes [key];
  324. Attributes.Remove (key);
  325. }
  326. if (Attributes.Count > 0) {
  327. Attributes.AddAttributes (writer);
  328. return true;
  329. }
  330. return false;
  331. }
  332. #if NET_2_0
  333. protected internal
  334. #else
  335. protected
  336. #endif
  337. override void Render (HtmlTextWriter w)
  338. {
  339. if (Page != null)
  340. Page.VerifyRenderingInServerForm (this);
  341. bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
  342. if (need_span) {
  343. #if NET_2_0
  344. AddDisplayStyleAttribute (w);
  345. #endif
  346. ControlStyle.AddAttributesToRender (w, this);
  347. }
  348. if (!Enabled) {
  349. w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  350. need_span = true;
  351. }
  352. string tt = ToolTip;
  353. if (tt != ""){
  354. w.AddAttribute ("title", tt);
  355. need_span = true;
  356. }
  357. #if NET_2_0
  358. if (HasAttributes && AddAttributesForSpan (w))
  359. need_span = true;
  360. #else
  361. if (Attributes.Count > 0 && AddAttributesForSpan (w))
  362. need_span = true;
  363. #endif
  364. if (need_span)
  365. w.RenderBeginTag (HtmlTextWriterTag.Span);
  366. TextAlign align = TextAlign;
  367. if (align == TextAlign.Right) {
  368. RenderInput (w);
  369. RenderLabel (w);
  370. } else {
  371. RenderLabel (w);
  372. RenderInput (w);
  373. }
  374. if (need_span)
  375. w.RenderEndTag ();
  376. }
  377. private void RenderInput (HtmlTextWriter w) {
  378. if (ClientID != null && ClientID.Length > 0)
  379. w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  380. w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
  381. string nameAttr = NameAttribute;
  382. if (nameAttr != null && nameAttr.Length > 0)
  383. w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
  384. InternalAddAttributesToRender (w);
  385. #if NET_2_0
  386. AddAttributesToRender (w);
  387. if (inputAttributes != null)
  388. inputAttributes.AddAttributes (w);
  389. #endif
  390. if (Checked)
  391. w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
  392. if (AutoPostBack) {
  393. #if NET_2_0
  394. string onclick = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
  395. onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
  396. w.AddAttribute (HtmlTextWriterAttribute.Onclick, BuildScriptAttribute ("onclick", onclick));
  397. #else
  398. w.AddAttribute (HtmlTextWriterAttribute.Onclick,
  399. BuildScriptAttribute ("onclick", Page.ClientScript.GetPostBackEventReference (this, String.Empty)));
  400. #endif
  401. w.AddAttribute ("language", "javascript", false);
  402. }
  403. if (AccessKey.Length > 0)
  404. w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  405. if (TabIndex != 0)
  406. w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  407. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  408. if (common_attrs != null)
  409. common_attrs.AddAttributes (w);
  410. w.RenderBeginTag (HtmlTextWriterTag.Input);
  411. w.RenderEndTag ();
  412. }
  413. private void RenderLabel (HtmlTextWriter w) {
  414. string text = Text;
  415. if (text.Length > 0) {
  416. #if NET_2_0
  417. if (labelAttributes != null)
  418. labelAttributes.AddAttributes (w);
  419. #endif
  420. w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  421. w.RenderBeginTag (HtmlTextWriterTag.Label);
  422. w.Write (text);
  423. w.RenderEndTag ();
  424. }
  425. }
  426. #if NET_2_0
  427. protected virtual
  428. #endif
  429. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  430. {
  431. if (!Enabled)
  432. return false;
  433. string postedValue = postCollection[postDataKey];
  434. bool postedBool = ((postedValue != null) &&
  435. (postedValue.Length > 0));
  436. if (Checked != postedBool) {
  437. Checked = postedBool;
  438. return (true);
  439. }
  440. return (false);
  441. }
  442. #if NET_2_0
  443. protected virtual
  444. #endif
  445. void RaisePostDataChangedEvent ()
  446. {
  447. #if NET_2_0
  448. if (CausesValidation)
  449. Page.Validate (ValidationGroup);
  450. #endif
  451. OnCheckedChanged (EventArgs.Empty);
  452. }
  453. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  454. {
  455. return LoadPostData (postDataKey, postCollection);
  456. }
  457. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  458. {
  459. RaisePostDataChangedEvent ();
  460. }
  461. #if NET_2_0
  462. PostBackOptions GetPostBackOptions () {
  463. PostBackOptions options = new PostBackOptions (this);
  464. options.ActionUrl = null;
  465. options.ValidationGroup = null;
  466. options.Argument = "";
  467. options.RequiresJavaScriptProtocol = false;
  468. options.ClientSubmit = true;
  469. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  470. if (options.PerformValidation)
  471. options.ValidationGroup = ValidationGroup;
  472. return options;
  473. }
  474. protected override void AddAttributesToRender (HtmlTextWriter writer)
  475. {
  476. }
  477. #endif
  478. internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
  479. {
  480. if (!Enabled)
  481. w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  482. }
  483. }
  484. }