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.
30 using System.Collections.Specialized;
31 using System.ComponentModel;
33 namespace System.Web.UI.WebControls {
34 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
35 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
36 [DefaultEvent ("CheckedChanged")]
37 [DefaultProperty ("Text")]
39 [ControlValueProperty ("Checked", null)]
41 public class CheckBox : WebControl, IPostBackDataHandler
49 AttributeCollection inputAttributes;
50 StateBag inputAttributesState;
51 AttributeCollection labelAttributes;
52 StateBag labelAttributesState;
55 public CheckBox () : base (HtmlTextWriterTag.Input)
57 render_type = "checkbox";
60 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
62 this.render_type = render_type;
65 [DefaultValue (false)]
69 [WebSysDescription ("")]
70 [WebCategory ("Behavior")]
71 public virtual bool AutoPostBack
74 return (ViewState.GetBool ("AutoPostBack",
78 ViewState["AutoPostBack"] = value;
83 [DefaultValue (false)]
86 [WebSysDescription ("")]
87 [WebCategory ("Behavior")]
88 public virtual bool CausesValidation
90 get { return ViewState.GetBool ("CausesValidation", false); }
91 set { ViewState ["CausesValidation"] = value; }
96 [DefaultValue (false)]
98 [Bindable (true, BindingDirection.TwoWay)]
103 [WebSysDescription ("")]
104 [WebCategory ("Behavior")]
105 public virtual bool Checked
108 return (ViewState.GetBool ("Checked", false));
111 ViewState["Checked"] = value;
117 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
118 public AttributeCollection InputAttributes
121 if (inputAttributes == null) {
122 if (inputAttributesState == null) {
123 inputAttributesState = new StateBag (true);
124 if (IsTrackingViewState)
125 inputAttributesState.TrackViewState();
127 inputAttributes = new AttributeCollection (inputAttributesState);
129 return inputAttributes;
134 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
135 public AttributeCollection LabelAttributes
138 if (labelAttributes == null) {
139 if (labelAttributesState == null) {
140 labelAttributesState = new StateBag (true);
141 if (IsTrackingViewState)
142 labelAttributesState.TrackViewState();
144 labelAttributes = new AttributeCollection (labelAttributesState);
146 return labelAttributes;
156 [WebSysDescription ("")]
157 [WebCategory ("Appearance")]
158 public virtual string Text
161 return (ViewState.GetString ("Text",
165 ViewState["Text"] = value;
169 [DefaultValue (TextAlign.Right)]
173 [WebSysDescription ("")]
174 [WebCategory ("Appearance")]
175 public virtual TextAlign TextAlign
178 object o = ViewState["TextAlign"];
181 return (TextAlign.Right);
183 return ((TextAlign)o);
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 string ValidationGroup
203 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
204 set { ViewState["ValidationGroup"] = value; }
208 private 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);
237 protected override void AddAttributesToRender (HtmlTextWriter w)
240 Page.VerifyRenderingInServerForm (this);
242 /* This is a nasty kludge to avoid rendering
243 * "style" and the ControlStyle in checkboxes
244 * (we use a surrounding <span> instead), but
245 * still pass the unit test that shows the
246 * style being rendered in multiple calls to
247 * Render () (which means we can't just delete
248 * Attributes["style"] or ControlStyle.Reset()
251 string css_style = Attributes ["style"];
252 if (css_style != null) {
253 Attributes.Remove ("style");
256 Style style = new Style ();
257 if (ControlStyleCreated) {
258 style.CopyFrom (ControlStyle);
259 ControlStyle.Reset ();
262 base.AddAttributesToRender (w);
264 if (css_style != null) {
265 Attributes ["style"] = css_style;
267 if (!style.IsEmpty) {
271 InternalAddAttributesToRender (w);
273 w.AddAttribute (HtmlTextWriterAttribute.Type,
275 w.AddAttribute (HtmlTextWriterAttribute.Name,
279 w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
283 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
288 protected override void LoadViewState (object savedState)
290 if (savedState == null) {
291 base.LoadViewState (null);
295 Triplet saved = (Triplet) savedState;
296 base.LoadViewState (saved.First);
298 if (saved.Second != null) {
299 if (inputAttributesState == null) {
300 inputAttributesState = new StateBag(true);
301 inputAttributesState.TrackViewState ();
303 inputAttributesState.LoadViewState (saved.Second);
306 if (saved.Third != null) {
307 if (labelAttributesState == null) {
308 labelAttributesState = new StateBag(true);
309 labelAttributesState.TrackViewState ();
311 labelAttributesState.LoadViewState (saved.Third);
315 protected override object SaveViewState ()
317 object baseView = base.SaveViewState ();
318 object inputAttrView = null;
319 object labelAttrView = null;
321 if (inputAttributesState != null)
322 inputAttrView = inputAttributesState.SaveViewState ();
324 if (labelAttributesState != null)
325 labelAttrView = labelAttributesState.SaveViewState ();
327 if (baseView == null && inputAttrView == null && labelAttrView == null)
330 return new Triplet (baseView, inputAttrView, labelAttrView);
333 protected override void TrackViewState ()
335 base.TrackViewState();
336 if (inputAttributesState != null)
337 inputAttributesState.TrackViewState ();
338 if (labelAttributesState != null)
339 labelAttributesState.TrackViewState ();
348 override void OnPreRender (EventArgs e)
350 base.OnPreRender (e);
353 Page.RegisterRequiresPostBack (this);
357 void RenderLabel (HtmlTextWriter w)
359 if (Text.Length > 0) {
361 if (labelAttributes != null)
362 labelAttributes.AddAttributes (w);
364 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
365 w.RenderBeginTag (HtmlTextWriterTag.Label);
376 override void Render (HtmlTextWriter w)
378 bool control_style = false;
380 /* Need to apply the styles around the text
383 if (ControlStyleCreated) {
384 ControlStyle.AddAttributesToRender (w, this);
385 w.RenderBeginTag (HtmlTextWriterTag.Span);
387 control_style = true;
388 } else if (Attributes ["style"] != null) {
389 /* TODO: check if this or the style
390 * has precendence, or if they should
391 * be merged (if I can figure out how
392 * to turn a CssStyleCollection into a
395 CssStyleCollection style = Attributes.CssStyle;
397 w.AddAttribute (HtmlTextWriterAttribute.Style,
399 w.RenderBeginTag (HtmlTextWriterTag.Span);
401 control_style = true;
404 if (TextAlign == TextAlign.Left) {
420 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
422 string postedValue = postCollection[postDataKey];
423 bool postedBool = ((postedValue != null) &&
424 (postedValue.Length > 0));
426 if (Checked != postedBool) {
427 Checked = postedBool;
437 void RaisePostDataChangedEvent ()
440 if (CausesValidation)
441 Page.Validate (ValidationGroup);
443 OnCheckedChanged (EventArgs.Empty);
446 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
448 return LoadPostData (postDataKey, postCollection);
451 void IPostBackDataHandler.RaisePostDataChangedEvent ()
453 RaisePostDataChangedEvent ();
456 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)