2 // System.Web.UI.WebControls.CheckBox.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 // Thanks to Leen Toelen (toelen@hotmail.com)'s classes that helped me
11 // to write the contents of the function LoadPostData(...)
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Globalization;
20 using System.ComponentModel;
21 using System.ComponentModel.Design;
23 namespace System.Web.UI.WebControls
25 [DefaultEvent("CheckedChanged")]
26 [DefaultProperty("Text")]
27 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
28 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
29 public class CheckBox : WebControl, IPostBackDataHandler
31 private static readonly object CheckedChangedEvent = new object();
33 public CheckBox(): base(HtmlTextWriterTag.Input)
37 [DefaultValue (false), WebCategory ("Behavior")]
38 [WebSysDescription ("The control automatically posts back after changing the text.")]
39 public virtual bool AutoPostBack
42 object o = ViewState ["AutoPostBack"];
43 return (o == null) ? false : (bool) o;
46 set { ViewState ["AutoPostBack"] = value; }
50 [DefaultValue (false), Bindable (true)]
51 [WebSysDescription ("Determines if the control is checked.")]
52 public virtual bool Checked
55 object o = ViewState ["Checked"];
56 return (o == null) ? false : (bool) o;
59 set { ViewState ["Checked"] = value; }
62 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
63 [WebSysDescription ("The text that this control displays.")]
64 public virtual string Text
67 object o = ViewState ["Text"];
68 return (o == null) ? String.Empty : (string) o;
71 set { ViewState ["Text"] = value; }
74 private bool SaveCheckedViewState
77 if (Events [CheckedChangedEvent] != null || !Enabled)
80 Type type = GetType ();
81 return (type != typeof (CheckBox) && type != typeof (RadioButton));
86 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
87 [WebSysDescription ("The alignment of the text.")]
88 public virtual TextAlign TextAlign
91 object o = ViewState ["TextAlign"];
92 return (o == null) ? TextAlign.Right : (TextAlign) o;
96 if (!System.Enum.IsDefined (typeof (TextAlign), value))
97 throw new ArgumentException ();
98 ViewState ["TextAlign"] = value;
102 [WebCategory ("Action")]
103 [WebSysDescription ("Raised when the control is checked or unchecked.")]
104 public event EventHandler CheckedChanged
106 add { Events.AddHandler (CheckedChangedEvent, value); }
107 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
110 protected virtual void OnCheckedChanged(EventArgs e)
113 EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
119 protected override void OnPreRender(EventArgs e)
121 if (Page != null && Enabled) {
122 Page.RegisterRequiresPostBack (this);
124 Page.RequiresPostBackScript ();
127 if (!SaveCheckedViewState)
128 ViewState.SetItemDirty ("Checked", false);
131 protected override void Render (HtmlTextWriter writer)
133 bool hasBeginRendering = false;
134 if(ControlStyleCreated && !ControlStyle.IsEmpty){
135 hasBeginRendering = true;
136 ControlStyle.AddAttributesToRender (writer, this);
141 hasBeginRendering = true;
\r
142 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
\r
145 if (ToolTip.Length > 0){
146 hasBeginRendering = true;
147 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
150 if (Attributes.Count > 0){
151 string val = Attributes ["value"];
152 Attributes.Remove ("value");
153 if (Attributes.Count > 0){
154 hasBeginRendering = true;
155 Attributes.AddAttributes (writer);
159 Attributes ["value"] = val;
162 if (hasBeginRendering)
163 writer.RenderBeginTag (HtmlTextWriterTag.Span);
165 if (Text.Length > 0){
166 TextAlign ta = TextAlign;
167 if(ta == TextAlign.Right)
168 RenderInputTag (writer, ClientID);
169 writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
170 writer.RenderBeginTag (HtmlTextWriterTag.Label);
172 writer.RenderEndTag ();
173 if(ta == TextAlign.Left)
174 RenderInputTag (writer, ClientID);
177 RenderInputTag (writer, ClientID);
179 if (hasBeginRendering)
180 writer.RenderEndTag ();
183 internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
186 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
188 writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
189 writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
190 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
192 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
195 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
196 Page.GetPostBackClientEvent (this, String.Empty));
197 writer.AddAttribute ("language", "javascript");
200 if (AccessKey.Length > 0)
201 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
204 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
205 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
207 writer.RenderBeginTag (HtmlTextWriterTag.Input);
208 writer.RenderEndTag ();
211 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
213 string postedVal = postCollection [postDataKey];
\r
214 bool haveData = ((postedVal != null)&& (postedVal.Length > 0));
\r
215 bool diff = (haveData != Checked);
220 void IPostBackDataHandler.RaisePostDataChangedEvent()
222 OnCheckedChanged (EventArgs.Empty);