CheckBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //
  2. // System.Web.UI.WebControls.CheckBox.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2005-2010 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. using System.Web.Util;
  34. namespace System.Web.UI.WebControls
  35. {
  36. // CAS
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. // attributes
  40. [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  42. [DefaultEvent ("CheckedChanged")]
  43. [DefaultProperty ("Text")]
  44. [ControlValueProperty ("Checked", null)]
  45. [SupportsEventValidation]
  46. public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
  47. {
  48. string render_type;
  49. AttributeCollection common_attrs;
  50. AttributeCollection inputAttributes;
  51. StateBag inputAttributesState;
  52. AttributeCollection labelAttributes;
  53. StateBag labelAttributesState;
  54. public CheckBox () : base (HtmlTextWriterTag.Input)
  55. {
  56. render_type = "checkbox";
  57. }
  58. internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
  59. {
  60. this.render_type = render_type;
  61. }
  62. [DefaultValue (false)]
  63. [Themeable (false)]
  64. [WebSysDescription ("")]
  65. [WebCategory ("Behavior")]
  66. public virtual bool AutoPostBack {
  67. get { return (ViewState.GetBool ("AutoPostBack", false)); }
  68. set { ViewState["AutoPostBack"] = value; }
  69. }
  70. [DefaultValue (false)]
  71. [Themeable (false)]
  72. [WebSysDescription ("")]
  73. [WebCategory ("Behavior")]
  74. public virtual bool CausesValidation {
  75. get { return ViewState.GetBool ("CausesValidation", false); }
  76. set { ViewState ["CausesValidation"] = value; }
  77. }
  78. [DefaultValue (false)]
  79. [Bindable (true, BindingDirection.TwoWay)]
  80. [Themeable (false)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Behavior")]
  83. public virtual bool Checked {
  84. get { return (ViewState.GetBool ("Checked", false)); }
  85. set { ViewState["Checked"] = value; }
  86. }
  87. [Browsable (false)]
  88. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  89. public AttributeCollection InputAttributes {
  90. get {
  91. if (inputAttributes == null) {
  92. if (inputAttributesState == null) {
  93. inputAttributesState = new StateBag (true);
  94. if (IsTrackingViewState)
  95. inputAttributesState.TrackViewState();
  96. }
  97. inputAttributes = new AttributeCollection (inputAttributesState);
  98. }
  99. return inputAttributes;
  100. }
  101. }
  102. [Browsable (false)]
  103. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  104. public AttributeCollection LabelAttributes {
  105. get {
  106. if (labelAttributes == null) {
  107. if (labelAttributesState == null) {
  108. labelAttributesState = new StateBag (true);
  109. if (IsTrackingViewState)
  110. labelAttributesState.TrackViewState();
  111. }
  112. labelAttributes = new AttributeCollection (labelAttributesState);
  113. }
  114. return labelAttributes;
  115. }
  116. }
  117. [DefaultValue ("")]
  118. [Bindable (true)]
  119. [Localizable (true)]
  120. [WebSysDescription ("")]
  121. [WebCategory ("Appearance")]
  122. public virtual string Text {
  123. get { return (ViewState.GetString ("Text", String.Empty)); }
  124. set { ViewState["Text"] = value; }
  125. }
  126. [DefaultValue (TextAlign.Right)]
  127. [WebSysDescription ("")]
  128. [WebCategory ("Appearance")]
  129. public virtual TextAlign TextAlign {
  130. get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
  131. set {
  132. if (value != TextAlign.Left &&
  133. value != TextAlign.Right) {
  134. throw new ArgumentOutOfRangeException ("value");
  135. }
  136. ViewState["TextAlign"] = value;
  137. }
  138. }
  139. [Themeable (false)]
  140. [DefaultValue ("")]
  141. [WebSysDescription ("")]
  142. [WebCategoryAttribute ("Behavior")]
  143. public virtual string ValidationGroup {
  144. get { return ViewState.GetString ("ValidationGroup", String.Empty); }
  145. set { ViewState["ValidationGroup"] = value; }
  146. }
  147. static readonly object EventCheckedChanged = new object ();
  148. [WebSysDescription ("")]
  149. [WebCategory ("Action")]
  150. public event EventHandler CheckedChanged {
  151. add { Events.AddHandler (EventCheckedChanged, value); }
  152. remove { Events.RemoveHandler (EventCheckedChanged, value); }
  153. }
  154. protected virtual void OnCheckedChanged (EventArgs e)
  155. {
  156. EventHandler handler = (EventHandler)Events[EventCheckedChanged];
  157. if (handler != null)
  158. handler (this, e);
  159. }
  160. internal virtual string NameAttribute {
  161. get { return (this.UniqueID); }
  162. }
  163. protected override void LoadViewState (object savedState)
  164. {
  165. if (savedState == null) {
  166. base.LoadViewState (null);
  167. return;
  168. }
  169. Triplet saved = (Triplet) savedState;
  170. base.LoadViewState (saved.First);
  171. if (saved.Second != null) {
  172. if (inputAttributesState == null) {
  173. inputAttributesState = new StateBag(true);
  174. inputAttributesState.TrackViewState ();
  175. }
  176. inputAttributesState.LoadViewState (saved.Second);
  177. }
  178. if (saved.Third != null) {
  179. if (labelAttributesState == null) {
  180. labelAttributesState = new StateBag(true);
  181. labelAttributesState.TrackViewState ();
  182. }
  183. labelAttributesState.LoadViewState (saved.Third);
  184. }
  185. }
  186. protected override object SaveViewState ()
  187. {
  188. object baseView = base.SaveViewState ();
  189. object inputAttrView = null;
  190. object labelAttrView = null;
  191. if (inputAttributesState != null)
  192. inputAttrView = inputAttributesState.SaveViewState ();
  193. if (labelAttributesState != null)
  194. labelAttrView = labelAttributesState.SaveViewState ();
  195. if (baseView == null && inputAttrView == null && labelAttrView == null)
  196. return null;
  197. return new Triplet (baseView, inputAttrView, labelAttrView);
  198. }
  199. protected override void TrackViewState ()
  200. {
  201. base.TrackViewState();
  202. if (inputAttributesState != null)
  203. inputAttributesState.TrackViewState ();
  204. if (labelAttributesState != null)
  205. labelAttributesState.TrackViewState ();
  206. }
  207. protected internal override void OnPreRender (EventArgs e)
  208. {
  209. base.OnPreRender (e);
  210. Page page = Page;
  211. if (page != null && IsEnabled) {
  212. page.RegisterRequiresPostBack (this);
  213. page.RegisterEnabledControl (this);
  214. }
  215. }
  216. static bool IsInputOrCommonAttr (string attname)
  217. {
  218. attname = attname.ToUpper (Helpers.InvariantCulture);
  219. switch (attname) {
  220. case "VALUE":
  221. case "CHECKED":
  222. case "SIZE":
  223. case "MAXLENGTH":
  224. case "SRC":
  225. case "ALT":
  226. case "USEMAP":
  227. case "DISABLED":
  228. case "READONLY":
  229. case "ACCEPT":
  230. case "ACCESSKEY":
  231. case "TABINDEX":
  232. case "ONFOCUS":
  233. case "ONBLUR":
  234. case "ONSELECT":
  235. case "ONCHANGE":
  236. case "ONCLICK":
  237. case "ONDBLCLICK":
  238. case "ONMOUSEDOWN":
  239. case "ONMOUSEUP":
  240. case "ONMOUSEOVER":
  241. case "ONMOUSEMOVE":
  242. case "ONMOUSEOUT":
  243. case "ONKEYPRESS":
  244. case "ONKEYDOWN":
  245. case "ONKEYUP":
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. bool AddAttributesForSpan (HtmlTextWriter writer)
  252. {
  253. if (HasAttributes) {
  254. AttributeCollection attributes = Attributes;
  255. ICollection k = attributes.Keys;
  256. string [] keys = new string [k.Count];
  257. k.CopyTo (keys, 0);
  258. foreach (string key in keys) {
  259. if (!IsInputOrCommonAttr (key))
  260. continue;
  261. if (common_attrs == null)
  262. common_attrs = new AttributeCollection (new StateBag ());
  263. common_attrs [key] = Attributes [key];
  264. attributes.Remove (key);
  265. }
  266. if (attributes.Count > 0) {
  267. attributes.AddAttributes (writer);
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. protected internal override void Render (HtmlTextWriter w)
  274. {
  275. Page page = Page;
  276. if (page != null) {
  277. page.VerifyRenderingInServerForm (this);
  278. page.ClientScript.RegisterForEventValidation (UniqueID);
  279. }
  280. bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
  281. bool enabled = IsEnabled;
  282. if (!enabled) {
  283. #if NET_4_0
  284. if (!RenderingCompatibilityLessThan40)
  285. ControlStyle.PrependCssClass (DisabledCssClass);
  286. else
  287. #endif
  288. w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  289. need_span = true;
  290. }
  291. if (need_span) {
  292. AddDisplayStyleAttribute (w);
  293. ControlStyle.AddAttributesToRender (w, this);
  294. }
  295. string tt = ToolTip;
  296. if (tt != null && tt.Length > 0){
  297. w.AddAttribute ("title", tt);
  298. need_span = true;
  299. }
  300. if (HasAttributes && AddAttributesForSpan (w))
  301. need_span = true;
  302. if (need_span)
  303. w.RenderBeginTag (HtmlTextWriterTag.Span);
  304. TextAlign align = TextAlign;
  305. if (align == TextAlign.Right) {
  306. RenderInput (w, enabled);
  307. RenderLabel (w);
  308. } else {
  309. RenderLabel (w);
  310. RenderInput (w, enabled);
  311. }
  312. if (need_span)
  313. w.RenderEndTag ();
  314. }
  315. void RenderInput (HtmlTextWriter w, bool enabled)
  316. {
  317. if (ClientID != null && ClientID.Length > 0)
  318. w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  319. w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
  320. string nameAttr = NameAttribute;
  321. if (nameAttr != null && nameAttr.Length > 0)
  322. w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
  323. InternalAddAttributesToRender (w, enabled);
  324. AddAttributesToRender (w);
  325. if (Checked)
  326. w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
  327. if (AutoPostBack) {
  328. Page page = Page;
  329. string onclick = page != null ? page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true) : String.Empty;
  330. onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
  331. w.AddAttribute (HtmlTextWriterAttribute.Onclick, BuildScriptAttribute ("onclick", onclick));
  332. }
  333. if (AccessKey.Length > 0)
  334. w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  335. if (TabIndex != 0)
  336. w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  337. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  338. if (common_attrs != null)
  339. common_attrs.AddAttributes (w);
  340. if (inputAttributes != null)
  341. inputAttributes.AddAttributes (w);
  342. w.RenderBeginTag (HtmlTextWriterTag.Input);
  343. w.RenderEndTag ();
  344. }
  345. void RenderLabel (HtmlTextWriter w)
  346. {
  347. string text = Text;
  348. if (text.Length > 0) {
  349. if (labelAttributes != null)
  350. labelAttributes.AddAttributes (w);
  351. w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  352. w.RenderBeginTag (HtmlTextWriterTag.Label);
  353. w.Write (text);
  354. w.RenderEndTag ();
  355. }
  356. }
  357. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  358. {
  359. if (!IsEnabled)
  360. return false;
  361. string postedValue = postCollection[postDataKey];
  362. bool postedBool = ((postedValue != null) &&
  363. (postedValue.Length > 0));
  364. if (Checked != postedBool) {
  365. Checked = postedBool;
  366. return (true);
  367. }
  368. return (false);
  369. }
  370. protected virtual void RaisePostDataChangedEvent ()
  371. {
  372. ValidateEvent (UniqueID, String.Empty);
  373. if (CausesValidation) {
  374. Page page = Page;
  375. if (page != null)
  376. page.Validate (ValidationGroup);
  377. }
  378. OnCheckedChanged (EventArgs.Empty);
  379. }
  380. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  381. {
  382. return LoadPostData (postDataKey, postCollection);
  383. }
  384. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  385. {
  386. RaisePostDataChangedEvent ();
  387. }
  388. PostBackOptions GetPostBackOptions ()
  389. {
  390. PostBackOptions options = new PostBackOptions (this);
  391. options.ActionUrl = null;
  392. options.ValidationGroup = null;
  393. options.Argument = String.Empty;
  394. options.RequiresJavaScriptProtocol = false;
  395. options.ClientSubmit = true;
  396. Page page = Page;
  397. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  398. if (options.PerformValidation)
  399. options.ValidationGroup = ValidationGroup;
  400. return options;
  401. }
  402. protected override void AddAttributesToRender (HtmlTextWriter writer)
  403. {
  404. }
  405. internal virtual void InternalAddAttributesToRender (HtmlTextWriter w, bool enabled)
  406. {
  407. if (!enabled)
  408. w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
  409. }
  410. }
  411. }