2 // System.Web.UI.WebControls.CheckBox.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005-2010 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
39 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
44 [DefaultEvent ("CheckedChanged")]
45 [DefaultProperty ("Text")]
46 [ControlValueProperty ("Checked", null)]
47 [SupportsEventValidation]
48 public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
51 AttributeCollection common_attrs;
52 AttributeCollection inputAttributes;
53 StateBag inputAttributesState;
54 AttributeCollection labelAttributes;
55 StateBag labelAttributesState;
57 public CheckBox () : base (HtmlTextWriterTag.Input)
59 render_type = "checkbox";
62 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
64 this.render_type = render_type;
67 [DefaultValue (false)]
69 [WebSysDescription ("")]
70 [WebCategory ("Behavior")]
71 public virtual bool AutoPostBack {
72 get { return (ViewState.GetBool ("AutoPostBack", false)); }
73 set { ViewState["AutoPostBack"] = value; }
76 [DefaultValue (false)]
78 [WebSysDescription ("")]
79 [WebCategory ("Behavior")]
80 public virtual bool CausesValidation {
81 get { return ViewState.GetBool ("CausesValidation", false); }
82 set { ViewState ["CausesValidation"] = value; }
85 [DefaultValue (false)]
86 [Bindable (true, BindingDirection.TwoWay)]
88 [WebSysDescription ("")]
89 [WebCategory ("Behavior")]
90 public virtual bool Checked {
91 get { return (ViewState.GetBool ("Checked", false)); }
92 set { ViewState["Checked"] = value; }
96 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
97 public AttributeCollection InputAttributes {
99 if (inputAttributes == null) {
100 if (inputAttributesState == null) {
101 inputAttributesState = new StateBag (true);
102 if (IsTrackingViewState)
103 inputAttributesState.TrackViewState();
105 inputAttributes = new AttributeCollection (inputAttributesState);
107 return inputAttributes;
112 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113 public AttributeCollection LabelAttributes {
115 if (labelAttributes == null) {
116 if (labelAttributesState == null) {
117 labelAttributesState = new StateBag (true);
118 if (IsTrackingViewState)
119 labelAttributesState.TrackViewState();
121 labelAttributes = new AttributeCollection (labelAttributesState);
123 return labelAttributes;
130 [WebSysDescription ("")]
131 [WebCategory ("Appearance")]
132 public virtual string Text {
133 get { return (ViewState.GetString ("Text", String.Empty)); }
134 set { ViewState["Text"] = value; }
137 [DefaultValue (TextAlign.Right)]
138 [WebSysDescription ("")]
139 [WebCategory ("Appearance")]
140 public virtual TextAlign TextAlign {
141 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
143 if (value != TextAlign.Left &&
144 value != TextAlign.Right) {
145 throw new ArgumentOutOfRangeException ("value");
148 ViewState["TextAlign"] = value;
154 [WebSysDescription ("")]
155 [WebCategoryAttribute ("Behavior")]
156 public virtual string ValidationGroup {
157 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
158 set { ViewState["ValidationGroup"] = value; }
161 static readonly object EventCheckedChanged = new object ();
162 [WebSysDescription ("")]
163 [WebCategory ("Action")]
164 public event EventHandler CheckedChanged {
165 add { Events.AddHandler (EventCheckedChanged, value); }
166 remove { Events.RemoveHandler (EventCheckedChanged, value); }
169 protected virtual void OnCheckedChanged (EventArgs e)
171 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
177 internal virtual string NameAttribute {
178 get { return (this.UniqueID); }
181 protected override void LoadViewState (object savedState)
183 if (savedState == null) {
184 base.LoadViewState (null);
188 Triplet saved = (Triplet) savedState;
189 base.LoadViewState (saved.First);
191 if (saved.Second != null) {
192 if (inputAttributesState == null) {
193 inputAttributesState = new StateBag(true);
194 inputAttributesState.TrackViewState ();
196 inputAttributesState.LoadViewState (saved.Second);
199 if (saved.Third != null) {
200 if (labelAttributesState == null) {
201 labelAttributesState = new StateBag(true);
202 labelAttributesState.TrackViewState ();
204 labelAttributesState.LoadViewState (saved.Third);
208 protected override object SaveViewState ()
210 object baseView = base.SaveViewState ();
211 object inputAttrView = null;
212 object labelAttrView = null;
214 if (inputAttributesState != null)
215 inputAttrView = inputAttributesState.SaveViewState ();
217 if (labelAttributesState != null)
218 labelAttrView = labelAttributesState.SaveViewState ();
220 if (baseView == null && inputAttrView == null && labelAttrView == null)
223 return new Triplet (baseView, inputAttrView, labelAttrView);
226 protected override void TrackViewState ()
228 base.TrackViewState();
229 if (inputAttributesState != null)
230 inputAttributesState.TrackViewState ();
231 if (labelAttributesState != null)
232 labelAttributesState.TrackViewState ();
235 protected internal override void OnPreRender (EventArgs e)
237 base.OnPreRender (e);
240 if (page != null && IsEnabled) {
241 page.RegisterRequiresPostBack (this);
242 page.RegisterEnabledControl (this);
246 static bool IsInputOrCommonAttr (string attname)
248 attname = attname.ToUpper (Helpers.InvariantCulture);
282 bool AddAttributesForSpan (HtmlTextWriter writer)
285 AttributeCollection attributes = Attributes;
286 ICollection k = attributes.Keys;
287 string [] keys = new string [k.Count];
289 foreach (string key in keys) {
290 if (!IsInputOrCommonAttr (key))
292 if (common_attrs == null)
293 common_attrs = new AttributeCollection (new StateBag ());
294 common_attrs [key] = Attributes [key];
295 attributes.Remove (key);
298 if (attributes.Count > 0) {
299 attributes.AddAttributes (writer);
307 protected internal override void Render (HtmlTextWriter w)
311 page.VerifyRenderingInServerForm (this);
312 page.ClientScript.RegisterForEventValidation (UniqueID);
315 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
316 bool enabled = IsEnabled;
318 if (!RenderingCompatibilityLessThan40)
319 ControlStyle.PrependCssClass (DisabledCssClass);
321 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
326 AddDisplayStyleAttribute (w);
327 ControlStyle.AddAttributesToRender (w, this);
331 if (tt != null && tt.Length > 0){
332 w.AddAttribute ("title", tt);
336 if (HasAttributes && AddAttributesForSpan (w))
340 w.RenderBeginTag (HtmlTextWriterTag.Span);
342 TextAlign align = TextAlign;
343 if (align == TextAlign.Right) {
344 RenderInput (w, enabled);
348 RenderInput (w, enabled);
355 void RenderInput (HtmlTextWriter w, bool enabled)
357 if (ClientID != null && ClientID.Length > 0)
358 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
359 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
360 string nameAttr = NameAttribute;
361 if (nameAttr != null && nameAttr.Length > 0)
362 w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
363 InternalAddAttributesToRender (w, enabled);
364 AddAttributesToRender (w);
367 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
371 string onclick = page != null ? page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true) : String.Empty;
372 onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
373 if (common_attrs != null && common_attrs ["onclick"] != null) {
374 onclick = ClientScriptManager.EnsureEndsWithSemicolon (common_attrs ["onclick"]) + onclick;
375 common_attrs.Remove ("onclick");
377 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
380 if (AccessKey.Length > 0)
381 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
384 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
385 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
387 if (common_attrs != null)
388 common_attrs.AddAttributes (w);
390 if (inputAttributes != null)
391 inputAttributes.AddAttributes (w);
393 w.RenderBeginTag (HtmlTextWriterTag.Input);
397 void RenderLabel (HtmlTextWriter w)
400 if (text.Length > 0) {
401 if (labelAttributes != null)
402 labelAttributes.AddAttributes (w);
403 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
404 w.RenderBeginTag (HtmlTextWriterTag.Label);
410 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
415 string postedValue = postCollection[postDataKey];
416 bool postedBool = ((postedValue != null) &&
417 (postedValue.Length > 0));
419 if (Checked != postedBool) {
420 Checked = postedBool;
427 protected virtual void RaisePostDataChangedEvent ()
429 ValidateEvent (UniqueID, String.Empty);
430 if (CausesValidation) {
433 page.Validate (ValidationGroup);
436 OnCheckedChanged (EventArgs.Empty);
439 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
441 return LoadPostData (postDataKey, postCollection);
444 void IPostBackDataHandler.RaisePostDataChangedEvent ()
446 RaisePostDataChangedEvent ();
449 PostBackOptions GetPostBackOptions ()
451 PostBackOptions options = new PostBackOptions (this);
452 options.ActionUrl = null;
453 options.ValidationGroup = null;
454 options.Argument = String.Empty;
455 options.RequiresJavaScriptProtocol = false;
456 options.ClientSubmit = true;
459 options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
460 if (options.PerformValidation)
461 options.ValidationGroup = ValidationGroup;
466 protected override void AddAttributesToRender (HtmlTextWriter writer)
470 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w, bool enabled)
473 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);