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;
35 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
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")]
45 [ControlValueProperty ("Checked", null)]
46 [SupportsEventValidation]
48 public class CheckBox : WebControl, IPostBackDataHandler
54 AttributeCollection common_attrs;
57 AttributeCollection inputAttributes;
58 StateBag inputAttributesState;
59 AttributeCollection labelAttributes;
60 StateBag labelAttributesState;
63 public CheckBox () : base (HtmlTextWriterTag.Input)
65 render_type = "checkbox";
68 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
70 this.render_type = render_type;
73 [DefaultValue (false)]
77 [WebSysDescription ("")]
78 [WebCategory ("Behavior")]
79 public virtual bool AutoPostBack
82 return (ViewState.GetBool ("AutoPostBack",
86 ViewState["AutoPostBack"] = value;
91 [DefaultValue (false)]
93 [WebSysDescription ("")]
94 [WebCategory ("Behavior")]
95 public virtual bool CausesValidation
97 get { return ViewState.GetBool ("CausesValidation", false); }
98 set { ViewState ["CausesValidation"] = value; }
103 [DefaultValue (false)]
105 [Bindable (true, BindingDirection.TwoWay)]
110 [WebSysDescription ("")]
111 [WebCategory ("Behavior")]
112 public virtual bool Checked
115 return (ViewState.GetBool ("Checked", false));
118 ViewState["Checked"] = value;
124 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
125 public AttributeCollection InputAttributes
128 if (inputAttributes == null) {
129 if (inputAttributesState == null) {
130 inputAttributesState = new StateBag (true);
131 if (IsTrackingViewState)
132 inputAttributesState.TrackViewState();
134 inputAttributes = new AttributeCollection (inputAttributesState);
136 return inputAttributes;
141 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
142 public AttributeCollection LabelAttributes
145 if (labelAttributes == null) {
146 if (labelAttributesState == null) {
147 labelAttributesState = new StateBag (true);
148 if (IsTrackingViewState)
149 labelAttributesState.TrackViewState();
151 labelAttributes = new AttributeCollection (labelAttributesState);
153 return labelAttributes;
163 [WebSysDescription ("")]
164 [WebCategory ("Appearance")]
165 public virtual string Text
168 return (ViewState.GetString ("Text",
172 ViewState["Text"] = value;
176 [DefaultValue (TextAlign.Right)]
180 [WebSysDescription ("")]
181 [WebCategory ("Appearance")]
182 public virtual TextAlign TextAlign
184 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
186 if (value != TextAlign.Left &&
187 value != TextAlign.Right) {
188 throw new ArgumentOutOfRangeException ("value");
191 ViewState["TextAlign"] = value;
198 [WebSysDescription ("")]
199 [WebCategoryAttribute ("Behavior")]
200 public virtual string ValidationGroup
202 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
203 set { ViewState["ValidationGroup"] = value; }
207 static readonly object EventCheckedChanged = new object ();
208 [WebSysDescription ("")]
209 [WebCategory ("Action")]
210 public event EventHandler CheckedChanged
213 Events.AddHandler (EventCheckedChanged, value);
216 Events.RemoveHandler (EventCheckedChanged, value);
220 protected virtual void OnCheckedChanged (EventArgs e)
222 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
224 if (handler != null) {
229 internal virtual string NameAttribute
232 return (this.UniqueID);
237 protected override void LoadViewState (object savedState)
239 if (savedState == null) {
240 base.LoadViewState (null);
244 Triplet saved = (Triplet) savedState;
245 base.LoadViewState (saved.First);
247 if (saved.Second != null) {
248 if (inputAttributesState == null) {
249 inputAttributesState = new StateBag(true);
250 inputAttributesState.TrackViewState ();
252 inputAttributesState.LoadViewState (saved.Second);
255 if (saved.Third != null) {
256 if (labelAttributesState == null) {
257 labelAttributesState = new StateBag(true);
258 labelAttributesState.TrackViewState ();
260 labelAttributesState.LoadViewState (saved.Third);
264 protected override object SaveViewState ()
266 object baseView = base.SaveViewState ();
267 object inputAttrView = null;
268 object labelAttrView = null;
270 if (inputAttributesState != null)
271 inputAttrView = inputAttributesState.SaveViewState ();
273 if (labelAttributesState != null)
274 labelAttrView = labelAttributesState.SaveViewState ();
276 if (baseView == null && inputAttrView == null && labelAttrView == null)
279 return new Triplet (baseView, inputAttrView, labelAttrView);
282 protected override void TrackViewState ()
284 base.TrackViewState();
285 if (inputAttributesState != null)
286 inputAttributesState.TrackViewState ();
287 if (labelAttributesState != null)
288 labelAttributesState.TrackViewState ();
297 override void OnPreRender (EventArgs e)
299 base.OnPreRender (e);
301 bool enabled = Enabled;
303 if (page != null && enabled) {
304 page.RegisterRequiresPostBack (this);
307 if (Page != null && enabled)
308 page.RegisterEnabledControl (this);
312 static bool IsInputOrCommonAttr (string attname)
314 attname = attname.ToUpper (CultureInfo.InvariantCulture);
348 bool AddAttributesForSpan (HtmlTextWriter writer)
350 ICollection k = Attributes.Keys;
351 string [] keys = new string [k.Count];
353 foreach (string key in keys) {
354 if (!IsInputOrCommonAttr (key))
356 if (common_attrs == null)
357 common_attrs = new AttributeCollection (new StateBag ());
358 common_attrs [key] = Attributes [key];
359 Attributes.Remove (key);
361 if (Attributes.Count > 0) {
362 Attributes.AddAttributes (writer);
372 override void Render (HtmlTextWriter w)
376 page.VerifyRenderingInServerForm (this);
378 page.ClientScript.RegisterForEventValidation (UniqueID);
382 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
385 AddDisplayStyleAttribute (w);
387 ControlStyle.AddAttributesToRender (w, this);
391 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
396 if (tt != null && tt.Length > 0){
397 w.AddAttribute ("title", tt);
402 if (HasAttributes && AddAttributesForSpan (w))
405 if (Attributes.Count > 0 && AddAttributesForSpan (w))
410 w.RenderBeginTag (HtmlTextWriterTag.Span);
412 TextAlign align = TextAlign;
413 if (align == TextAlign.Right) {
425 void RenderInput (HtmlTextWriter w) {
427 if (ClientID != null && ClientID.Length > 0)
428 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
429 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
430 string nameAttr = NameAttribute;
431 if (nameAttr != null && nameAttr.Length > 0)
432 w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
433 InternalAddAttributesToRender (w);
435 AddAttributesToRender (w);
436 if (inputAttributes != null)
437 inputAttributes.AddAttributes (w);
440 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
444 string onclick = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
445 onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
446 w.AddAttribute (HtmlTextWriterAttribute.Onclick, BuildScriptAttribute ("onclick", onclick));
448 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
449 BuildScriptAttribute ("onclick", Page.ClientScript.GetPostBackEventReference (this, String.Empty)));
450 w.AddAttribute ("language", "javascript", false);
454 if (AccessKey.Length > 0)
455 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
458 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
459 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
461 if (common_attrs != null)
462 common_attrs.AddAttributes (w);
463 w.RenderBeginTag (HtmlTextWriterTag.Input);
467 void RenderLabel (HtmlTextWriter w) {
469 if (text.Length > 0) {
471 if (labelAttributes != null)
472 labelAttributes.AddAttributes (w);
474 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
475 w.RenderBeginTag (HtmlTextWriterTag.Label);
484 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
489 string postedValue = postCollection[postDataKey];
490 bool postedBool = ((postedValue != null) &&
491 (postedValue.Length > 0));
493 if (Checked != postedBool) {
494 Checked = postedBool;
504 void RaisePostDataChangedEvent ()
507 ValidateEvent (UniqueID, String.Empty);
508 if (CausesValidation)
509 Page.Validate (ValidationGroup);
511 OnCheckedChanged (EventArgs.Empty);
514 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
516 return LoadPostData (postDataKey, postCollection);
519 void IPostBackDataHandler.RaisePostDataChangedEvent ()
521 RaisePostDataChangedEvent ();
525 PostBackOptions GetPostBackOptions () {
526 PostBackOptions options = new PostBackOptions (this);
527 options.ActionUrl = null;
528 options.ValidationGroup = null;
529 options.Argument = String.Empty;
530 options.RequiresJavaScriptProtocol = false;
531 options.ClientSubmit = true;
532 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
533 if (options.PerformValidation)
534 options.ValidationGroup = ValidationGroup;
539 protected override void AddAttributesToRender (HtmlTextWriter writer)
544 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
547 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);