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 private 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 if (Page != null && Enabled) {
302 Page.RegisterRequiresPostBack (this);
305 if (Page != null && Enabled)
306 Page.RegisterEnabledControl (this);
310 static bool IsInputOrCommonAttr (string attname)
312 attname = attname.ToUpper (CultureInfo.InvariantCulture);
346 bool AddAttributesForSpan (HtmlTextWriter writer)
348 ICollection k = Attributes.Keys;
349 string [] keys = new string [k.Count];
351 foreach (string key in keys) {
352 if (!IsInputOrCommonAttr (key))
354 if (common_attrs == null)
355 common_attrs = new AttributeCollection (new StateBag ());
356 common_attrs [key] = Attributes [key];
357 Attributes.Remove (key);
359 if (Attributes.Count > 0) {
360 Attributes.AddAttributes (writer);
370 override void Render (HtmlTextWriter w)
373 Page.VerifyRenderingInServerForm (this);
375 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
378 AddDisplayStyleAttribute (w);
380 ControlStyle.AddAttributesToRender (w, this);
384 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
390 w.AddAttribute ("title", tt);
394 if (Attributes.Count > 0 && AddAttributesForSpan (w))
398 w.RenderBeginTag (HtmlTextWriterTag.Span);
400 TextAlign align = TextAlign;
401 if (align == TextAlign.Right) {
413 private void RenderInput (HtmlTextWriter w) {
415 if (ClientID != null && ClientID.Length > 0)
416 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
417 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
418 string nameAttr = NameAttribute;
419 if (nameAttr != null && nameAttr.Length > 0)
420 w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
421 InternalAddAttributesToRender (w);
423 AddAttributesToRender (w);
424 if (inputAttributes != null)
425 inputAttributes.AddAttributes (w);
428 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
432 w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true));
434 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
435 Page.ClientScript.GetPostBackEventReference (this, String.Empty));
437 w.AddAttribute ("language", "javascript");
440 if (AccessKey.Length > 0)
441 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
444 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
445 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
447 if (common_attrs != null)
448 common_attrs.AddAttributes (w);
449 w.RenderBeginTag (HtmlTextWriterTag.Input);
453 private void RenderLabel (HtmlTextWriter w) {
455 if (text.Length > 0) {
457 if (labelAttributes != null)
458 labelAttributes.AddAttributes (w);
460 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
461 w.RenderBeginTag (HtmlTextWriterTag.Label);
470 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
475 string postedValue = postCollection[postDataKey];
476 bool postedBool = ((postedValue != null) &&
477 (postedValue.Length > 0));
479 if (Checked != postedBool) {
480 Checked = postedBool;
490 void RaisePostDataChangedEvent ()
493 if (CausesValidation)
494 Page.Validate (ValidationGroup);
496 OnCheckedChanged (EventArgs.Empty);
499 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
501 return LoadPostData (postDataKey, postCollection);
504 void IPostBackDataHandler.RaisePostDataChangedEvent ()
506 RaisePostDataChangedEvent ();
510 PostBackOptions GetPostBackOptions () {
511 PostBackOptions options = new PostBackOptions (this);
512 options.ActionUrl = null;
513 options.ValidationGroup = null;
514 options.Argument = "";
515 options.RequiresJavaScriptProtocol = false;
516 options.ClientSubmit = true;
517 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
518 if (options.PerformValidation)
519 options.ValidationGroup = ValidationGroup;
524 protected override void AddAttributesToRender (HtmlTextWriter writer)
529 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
532 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");