2 // System.Web.UI.WebControls.CheckBox.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Web.Util;
36 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
43 [DefaultEvent ("CheckedChanged")]
44 [DefaultProperty ("Text")]
46 [ControlValueProperty ("Checked", null)]
47 [SupportsEventValidation]
49 public class CheckBox : WebControl, IPostBackDataHandler
55 AttributeCollection common_attrs;
58 AttributeCollection inputAttributes;
59 StateBag inputAttributesState;
60 AttributeCollection labelAttributes;
61 StateBag labelAttributesState;
64 public CheckBox () : base (HtmlTextWriterTag.Input)
66 render_type = "checkbox";
69 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
71 this.render_type = render_type;
74 [DefaultValue (false)]
78 [WebSysDescription ("")]
79 [WebCategory ("Behavior")]
80 public virtual bool AutoPostBack
83 return (ViewState.GetBool ("AutoPostBack",
87 ViewState["AutoPostBack"] = value;
92 [DefaultValue (false)]
94 [WebSysDescription ("")]
95 [WebCategory ("Behavior")]
96 public virtual bool CausesValidation
98 get { return ViewState.GetBool ("CausesValidation", false); }
99 set { ViewState ["CausesValidation"] = value; }
104 [DefaultValue (false)]
106 [Bindable (true, BindingDirection.TwoWay)]
111 [WebSysDescription ("")]
112 [WebCategory ("Behavior")]
113 public virtual bool Checked
116 return (ViewState.GetBool ("Checked", false));
119 ViewState["Checked"] = value;
125 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
126 public AttributeCollection InputAttributes
129 if (inputAttributes == null) {
130 if (inputAttributesState == null) {
131 inputAttributesState = new StateBag (true);
132 if (IsTrackingViewState)
133 inputAttributesState.TrackViewState();
135 inputAttributes = new AttributeCollection (inputAttributesState);
137 return inputAttributes;
142 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
143 public AttributeCollection LabelAttributes
146 if (labelAttributes == null) {
147 if (labelAttributesState == null) {
148 labelAttributesState = new StateBag (true);
149 if (IsTrackingViewState)
150 labelAttributesState.TrackViewState();
152 labelAttributes = new AttributeCollection (labelAttributesState);
154 return labelAttributes;
164 [WebSysDescription ("")]
165 [WebCategory ("Appearance")]
166 public virtual string Text
169 return (ViewState.GetString ("Text",
173 ViewState["Text"] = value;
177 [DefaultValue (TextAlign.Right)]
181 [WebSysDescription ("")]
182 [WebCategory ("Appearance")]
183 public virtual TextAlign TextAlign
185 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
187 if (value != TextAlign.Left &&
188 value != TextAlign.Right) {
189 throw new ArgumentOutOfRangeException ("value");
192 ViewState["TextAlign"] = value;
199 [WebSysDescription ("")]
200 [WebCategoryAttribute ("Behavior")]
201 public virtual string ValidationGroup
203 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
204 set { ViewState["ValidationGroup"] = value; }
208 static readonly object EventCheckedChanged = new object ();
209 [WebSysDescription ("")]
210 [WebCategory ("Action")]
211 public event EventHandler CheckedChanged
214 Events.AddHandler (EventCheckedChanged, value);
217 Events.RemoveHandler (EventCheckedChanged, value);
221 protected virtual void OnCheckedChanged (EventArgs e)
223 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
225 if (handler != null) {
230 internal virtual string NameAttribute
233 return (this.UniqueID);
238 protected override void LoadViewState (object savedState)
240 if (savedState == null) {
241 base.LoadViewState (null);
245 Triplet saved = (Triplet) savedState;
246 base.LoadViewState (saved.First);
248 if (saved.Second != null) {
249 if (inputAttributesState == null) {
250 inputAttributesState = new StateBag(true);
251 inputAttributesState.TrackViewState ();
253 inputAttributesState.LoadViewState (saved.Second);
256 if (saved.Third != null) {
257 if (labelAttributesState == null) {
258 labelAttributesState = new StateBag(true);
259 labelAttributesState.TrackViewState ();
261 labelAttributesState.LoadViewState (saved.Third);
265 protected override object SaveViewState ()
267 object baseView = base.SaveViewState ();
268 object inputAttrView = null;
269 object labelAttrView = null;
271 if (inputAttributesState != null)
272 inputAttrView = inputAttributesState.SaveViewState ();
274 if (labelAttributesState != null)
275 labelAttrView = labelAttributesState.SaveViewState ();
277 if (baseView == null && inputAttrView == null && labelAttrView == null)
280 return new Triplet (baseView, inputAttrView, labelAttrView);
283 protected override void TrackViewState ()
285 base.TrackViewState();
286 if (inputAttributesState != null)
287 inputAttributesState.TrackViewState ();
288 if (labelAttributesState != null)
289 labelAttributesState.TrackViewState ();
298 override void OnPreRender (EventArgs e)
300 base.OnPreRender (e);
302 bool enabled = Enabled;
304 if (page != null && enabled) {
305 page.RegisterRequiresPostBack (this);
308 if (Page != null && enabled)
309 page.RegisterEnabledControl (this);
313 static bool IsInputOrCommonAttr (string attname)
315 attname = attname.ToUpper (Helpers.InvariantCulture);
349 bool AddAttributesForSpan (HtmlTextWriter writer)
352 AttributeCollection attributes = Attributes;
353 ICollection k = attributes.Keys;
354 string [] keys = new string [k.Count];
356 foreach (string key in keys) {
357 if (!IsInputOrCommonAttr (key))
359 if (common_attrs == null)
360 common_attrs = new AttributeCollection (new StateBag ());
361 common_attrs [key] = Attributes [key];
362 attributes.Remove (key);
365 if (attributes.Count > 0) {
366 attributes.AddAttributes (writer);
378 override void Render (HtmlTextWriter w)
382 page.VerifyRenderingInServerForm (this);
384 page.ClientScript.RegisterForEventValidation (UniqueID);
388 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
391 AddDisplayStyleAttribute (w);
393 ControlStyle.AddAttributesToRender (w, this);
397 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
402 if (tt != null && tt.Length > 0){
403 w.AddAttribute ("title", tt);
408 if (HasAttributes && AddAttributesForSpan (w))
411 if (Attributes.Count > 0 && AddAttributesForSpan (w))
416 w.RenderBeginTag (HtmlTextWriterTag.Span);
418 TextAlign align = TextAlign;
419 if (align == TextAlign.Right) {
431 void RenderInput (HtmlTextWriter w) {
433 if (ClientID != null && ClientID.Length > 0)
434 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
435 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
436 string nameAttr = NameAttribute;
437 if (nameAttr != null && nameAttr.Length > 0)
438 w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
439 InternalAddAttributesToRender (w);
441 AddAttributesToRender (w);
442 if (inputAttributes != null)
443 inputAttributes.AddAttributes (w);
446 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
450 string onclick = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
451 onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
452 w.AddAttribute (HtmlTextWriterAttribute.Onclick, BuildScriptAttribute ("onclick", onclick));
454 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
455 BuildScriptAttribute ("onclick", Page.ClientScript.GetPostBackEventReference (this, String.Empty)));
456 w.AddAttribute ("language", "javascript", false);
460 if (AccessKey.Length > 0)
461 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
464 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
465 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
467 if (common_attrs != null)
468 common_attrs.AddAttributes (w);
469 w.RenderBeginTag (HtmlTextWriterTag.Input);
473 void RenderLabel (HtmlTextWriter w) {
475 if (text.Length > 0) {
477 if (labelAttributes != null)
478 labelAttributes.AddAttributes (w);
480 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
481 w.RenderBeginTag (HtmlTextWriterTag.Label);
490 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
495 string postedValue = postCollection[postDataKey];
496 bool postedBool = ((postedValue != null) &&
497 (postedValue.Length > 0));
499 if (Checked != postedBool) {
500 Checked = postedBool;
510 void RaisePostDataChangedEvent ()
513 ValidateEvent (UniqueID, String.Empty);
514 if (CausesValidation)
515 Page.Validate (ValidationGroup);
517 OnCheckedChanged (EventArgs.Empty);
520 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
522 return LoadPostData (postDataKey, postCollection);
525 void IPostBackDataHandler.RaisePostDataChangedEvent ()
527 RaisePostDataChangedEvent ();
531 PostBackOptions GetPostBackOptions () {
532 PostBackOptions options = new PostBackOptions (this);
533 options.ActionUrl = null;
534 options.ValidationGroup = null;
535 options.Argument = String.Empty;
536 options.RequiresJavaScriptProtocol = false;
537 options.ClientSubmit = true;
538 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
539 if (options.PerformValidation)
540 options.ValidationGroup = ValidationGroup;
545 protected override void AddAttributesToRender (HtmlTextWriter writer)
550 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
553 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);