* CheckBox.cs: Do not change the status of a checkbox when there
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
1 //
2 // System.Web.UI.WebControls.CheckBox.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
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(...)
12 //
13
14 using System;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Globalization;
18 using System.Web;
19 using System.Web.UI;
20 using System.ComponentModel;
21 using System.ComponentModel.Design;
22
23 namespace System.Web.UI.WebControls
24 {
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
30         {
31                 private static readonly object CheckedChangedEvent = new object();
32                 
33                 public CheckBox(): base(HtmlTextWriterTag.Input)
34                 {
35                 }
36
37                 [DefaultValue (false), WebCategory ("Behavior")]
38                 [WebSysDescription ("The control automatically posts back after changing the text.")]
39                 public virtual bool AutoPostBack
40                 {
41                         get {
42                                  object o = ViewState ["AutoPostBack"];
43                                  return (o == null) ? false : (bool) o;
44                         }
45
46                         set { ViewState ["AutoPostBack"] = value; }
47                 }
48
49
50                 [DefaultValue (false), Bindable (true)]
51                 [WebSysDescription ("Determines if the control is checked.")]
52                 public virtual bool Checked
53                 {
54                         get {
55                                 object o = ViewState ["Checked"];
56                                 return (o == null) ? false : (bool) o;
57                         }
58
59                         set { ViewState ["Checked"] = value; }
60                 }
61
62                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
63                 [WebSysDescription ("The text that this control displays.")]
64                 public virtual string Text
65                 {
66                         get {
67                                 object o = ViewState ["Text"];
68                                 return (o == null) ? String.Empty : (string) o;
69                         }
70
71                         set { ViewState ["Text"] = value; }
72                 }
73                 
74                 private bool SaveCheckedViewState
75                 {
76                         get {
77                                 if (Events [CheckedChangedEvent] != null || !Enabled)
78                                         return true;
79
80                                 Type type = GetType ();
81                                 return (type != typeof (CheckBox) && type != typeof (RadioButton));
82                         }
83                 }
84
85
86                 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
87                 [WebSysDescription ("The alignment of the text.")]
88                 public virtual TextAlign TextAlign
89                 {
90                         get {
91                                 object o = ViewState ["TextAlign"];
92                                 return (o == null) ? TextAlign.Right : (TextAlign) o;
93                         }
94
95                         set {
96                                 if (!System.Enum.IsDefined (typeof (TextAlign), value))
97                                         throw new ArgumentException ();
98                                 ViewState ["TextAlign"] = value;
99                         }
100                 }
101
102                 [WebCategory ("Action")]
103                 [WebSysDescription ("Raised when the control is checked or unchecked.")]
104                 public event EventHandler CheckedChanged
105                 {
106                         add { Events.AddHandler (CheckedChangedEvent, value); }
107                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
108                 }
109                 
110                 protected virtual void OnCheckedChanged(EventArgs e)
111                 {
112                         if(Events != null){
113                                 EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
114                                 if(eh != null)
115                                         eh (this, e);
116                         }
117                 }
118                 
119                 protected override void OnPreRender(EventArgs e)
120                 {
121                         if (Page != null && Enabled) {
122                                 Page.RegisterRequiresPostBack (this);
123                                 if (AutoPostBack)
124                                         Page.RequiresPostBackScript ();
125                         }
126
127                         if (!SaveCheckedViewState)
128                                 ViewState.SetItemDirty ("Checked", false);
129                 }
130                 
131                 protected override void Render (HtmlTextWriter writer)
132                 {
133                         bool hasBeginRendering = false;
134                         if(ControlStyleCreated && !ControlStyle.IsEmpty){
135                                 hasBeginRendering = true;
136                                 ControlStyle.AddAttributesToRender (writer, this);
137                         }
138
139                         if (ToolTip.Length > 0){
140                                 hasBeginRendering = true;
141                                 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
142                         }
143
144                         if (Attributes.Count > 0){
145                                 string val = Attributes ["value"];
146                                 Attributes.Remove ("value");
147                                 if (Attributes.Count > 0){
148                                         hasBeginRendering = true;
149                                         Attributes.AddAttributes (writer);
150                                 }
151
152                                 if (val != null)
153                                         Attributes ["value"] = val;
154                         }
155
156                         if (hasBeginRendering)
157                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
158
159                         if (Text.Length > 0){
160                                 TextAlign ta = TextAlign;
161                                 if(ta == TextAlign.Right)
162                                         RenderInputTag (writer, ClientID);
163                                 writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
164                                 writer.RenderBeginTag (HtmlTextWriterTag.Label);
165                                 writer.Write (Text);
166                                 writer.RenderEndTag ();
167                                 if(ta == TextAlign.Left)
168                                         RenderInputTag (writer, ClientID);
169                         }
170                         else
171                                 RenderInputTag (writer, ClientID);
172
173                         if (hasBeginRendering)
174                                 writer.RenderEndTag ();
175                 }
176                 
177                 internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
178                 {
179                         if (!Enabled)
180                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
181
182                         writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
183                         writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
184                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
185                         if (Checked)
186                                 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
187
188                         if (AutoPostBack){
189                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
190                                                      Page.GetPostBackClientEvent (this, String.Empty));
191                                 writer.AddAttribute ("language", "javascript");
192                         }
193
194                         if (AccessKey.Length > 0)
195                                 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
196
197                         if (TabIndex != 0)
198                                 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
199                                                      TabIndex.ToString (NumberFormatInfo.InvariantInfo));
200
201                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
202                         writer.RenderEndTag ();
203                 }
204                 
205                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
206                 {
207                         string postedVal = postCollection [postDataKey];
208
209                         if (postedVal == null)
210                                 return false;
211                         
212                         bool haveData = (postedVal.Length > 0);
213                         if (haveData == Checked)
214                                 return false;
215
216                         Checked = haveData;
217                         return true;;
218                 }
219                 
220                 void IPostBackDataHandler.RaisePostDataChangedEvent()
221                 {
222                         OnCheckedChanged (EventArgs.Empty);
223                 }
224         }
225 }