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
48 public CheckBox () : base (HtmlTextWriterTag.Input)
50 render_type = "checkbox";
53 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
55 this.render_type = render_type;
58 [DefaultValue (false)]
62 public virtual bool AutoPostBack
65 return (ViewState.GetBool ("AutoPostBack",
69 ViewState["AutoPostBack"] = value;
74 [DefaultValue (false)]
77 public virtual bool CausesValidation
80 throw new NotImplementedException ();
83 throw new NotImplementedException ();
89 [DefaultValue (false)]
91 [Bindable (true, BindingDirection.TwoWay)]
96 public virtual bool Checked
99 return (ViewState.GetBool ("Checked", false));
102 ViewState["Checked"] = value;
108 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
110 public AttributeCollection InputAttributes
113 throw new NotImplementedException ();
116 throw new NotImplementedException ();
121 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
123 public AttributeCollection LabelAttributes
126 throw new NotImplementedException ();
129 throw new NotImplementedException ();
139 public virtual string Text
142 return (ViewState.GetString ("Text",
146 ViewState["Text"] = value;
150 [DefaultValue (TextAlign.Right)]
154 public virtual TextAlign TextAlign
157 object o = ViewState["TextAlign"];
160 return (TextAlign.Right);
162 return ((TextAlign)o);
166 if (value != TextAlign.Left &&
167 value != TextAlign.Right) {
168 throw new ArgumentOutOfRangeException ("value");
171 ViewState["TextAlign"] = value;
179 public string ValidationGroup
182 throw new NotImplementedException ();
185 throw new NotImplementedException ();
190 private static readonly object EventCheckedChanged = new object ();
191 public event EventHandler CheckedChanged
194 Events.AddHandler (EventCheckedChanged, value);
197 Events.RemoveHandler (EventCheckedChanged, value);
201 protected virtual void OnCheckedChanged (EventArgs e)
203 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
205 if (handler != null) {
210 internal virtual string NameAttribute
213 return (this.UniqueID);
217 protected override void AddAttributesToRender (HtmlTextWriter w)
220 Page.VerifyRenderingInServerForm (this);
222 /* This is a nasty kludge to avoid rendering
223 * "style" and the ControlStyle in checkboxes
224 * (we use a surrounding <span> instead), but
225 * still pass the unit test that shows the
226 * style being rendered in multiple calls to
227 * Render () (which means we can't just delete
228 * Attributes["style"] or ControlStyle.Reset()
231 string css_style = Attributes ["style"];
232 if (css_style != null) {
233 Attributes.Remove ("style");
236 Style style = new Style ();
237 if (ControlStyleCreated) {
238 style.CopyFrom (ControlStyle);
239 ControlStyle.Reset ();
242 base.AddAttributesToRender (w);
244 if (css_style != null) {
245 Attributes ["style"] = css_style;
247 if (!style.IsEmpty) {
251 InternalAddAttributesToRender (w);
253 w.AddAttribute (HtmlTextWriterAttribute.Type,
255 w.AddAttribute (HtmlTextWriterAttribute.Name,
259 w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
263 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
269 protected override void LoadViewState (object savedState)
271 throw new NotImplementedException ();
275 protected override object SaveViewState ()
277 throw new NotImplementedException ();
281 protected override void TrackViewState ()
283 throw new NotImplementedException ();
292 override void OnPreRender (EventArgs e)
294 base.OnPreRender (e);
297 Page.RegisterRequiresPostBack (this);
301 void RenderLabel (HtmlTextWriter w)
303 if (Text.Length > 0) {
304 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
305 w.RenderBeginTag (HtmlTextWriterTag.Label);
316 override void Render (HtmlTextWriter w)
318 bool control_style = false;
320 /* Need to apply the styles around the text
323 if (ControlStyleCreated) {
324 ControlStyle.AddAttributesToRender (w, this);
325 w.RenderBeginTag (HtmlTextWriterTag.Span);
327 control_style = true;
328 } else if (Attributes ["style"] != null) {
329 /* TODO: check if this or the style
330 * has precendence, or if they should
331 * be merged (if I can figure out how
332 * to turn a CssStyleCollection into a
335 CssStyleCollection style = Attributes.CssStyle;
337 w.AddAttribute (HtmlTextWriterAttribute.Style,
338 style.BagToString ());
339 w.RenderBeginTag (HtmlTextWriterTag.Span);
341 control_style = true;
344 if (TextAlign == TextAlign.Left) {
359 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
361 throw new NotImplementedException ();
365 protected virtual void RaisePostDataChangedEvent ()
367 throw new NotImplementedException ();
371 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
373 string postedValue = postCollection[postDataKey];
374 bool postedBool = ((postedValue != null) &&
375 (postedValue.Length > 0));
377 if (Checked != postedBool) {
378 Checked = postedBool;
385 void IPostBackDataHandler.RaisePostDataChangedEvent ()
387 OnCheckedChanged (EventArgs.Empty);
390 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)