2002-06-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
1 /**
2  * Namespace: System.Web.UI.WebControls
3  * Class:     CheckBox
4  *
5  * Author:  Gaurav Vaish
6  * Maintainer: gvaish@iitk.ac.in
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
8  * Implementation: yes
9  * Status:  100%
10  *
11  * (C) Gaurav Vaish (2001)
12  * Thanks to Leen Toelen (toelen@hotmail.com)'s classes that helped me
13  * to write the contents of the function LoadPostData(...)
14  */
15
16 using System;
17 using System.Collections;
18 using System.Collections.Specialized;
19 using System.Globalization;
20 using System.Web;
21 using System.Web.UI;
22 using System.ComponentModel;
23
24 namespace System.Web.UI.WebControls
25 {
26         [DefaultEvent("CheckedChanged")]
27         [DefaultProperty("Text")]
28         //[DataBindingHanlder("??")]
29         //[Designer("??")]
30         public class CheckBox : WebControl, IPostBackDataHandler
31         {
32                 private static readonly object CheckedChangedEvent = new object();
33                 
34                 public CheckBox(): base(HtmlTextWriterTag.Input)
35                 {
36                 }
37                 
38                 public virtual bool AutoPostBack
39                 {
40                         get {
41                                  object o = ViewState ["AutoPostBack"];
42                                  return (o == null) ? false : (bool) o;
43                         }
44
45                         set { ViewState ["AutoPostBack"] = value; }
46                 }
47                 
48                 public virtual bool Checked
49                 {
50                         get {
51                                 object o = ViewState ["Checked"];
52                                 return (o == null) ? false : (bool) o;
53                         }
54
55                         set { ViewState ["Checked"] = value; }
56                 }
57                 
58                 public virtual string Text
59                 {
60                         get {
61                                 object o = ViewState ["Text"];
62                                 return (o == null) ? String.Empty : (string) o;
63                         }
64
65                         set { ViewState ["Text"] = value; }
66                 }
67                 
68                 private bool SaveCheckedViewState
69                 {
70                         get {
71                                 if (Events [CheckedChangedEvent] != null){
72                                         if (!Enabled)
73                                                 return true;
74
75                                         Type type = GetType ();
76                                         if (type == typeof (CheckBox))
77                                                 return false;
78
79                                         if (type == typeof (RadioButton))
80                                                 return false;
81                                 }
82                                 return true;
83                         }
84                 }
85                 
86                 public virtual TextAlign TextAlign
87                 {
88                         get {
89                                 object o = ViewState ["TextAlign"];
90                                 return (o == null) ? TextAlign.Right : (TextAlign) o;
91                         }
92
93                         set {
94                                 if (!System.Enum.IsDefined (typeof (TextAlign), value))
95                                         throw new ArgumentException ();
96                                 ViewState ["TextAlign"] = value;
97                         }
98                 }
99                 
100                 public event EventHandler CheckedChanged
101                 {
102                         add { Events.AddHandler (CheckedChangedEvent, value); }
103                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
104                 }
105                 
106                 protected virtual void OnCheckedChanged(EventArgs e)
107                 {
108                         if(Events != null){
109                                 EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
110                                 if(eh != null)
111                                         eh (this, e);
112                         }
113                 }
114                 
115                 protected override void OnPreRender(EventArgs e)
116                 {
117                         if (Page != null && Enabled)
118                                 Page.RegisterRequiresPostBack (this);
119
120                         if (SaveCheckedViewState)
121                                 ViewState.SetItemDirty ("checked", false);
122                 }
123                 
124                 protected override void Render (HtmlTextWriter writer)
125                 {
126                         bool hasBeginRendering = false;
127                         if(ControlStyleCreated && !ControlStyle.IsEmpty){
128                                 hasBeginRendering = true;
129                                 ControlStyle.AddAttributesToRender (writer, this);
130                         }
131
132                         if (!Enabled){
133                                 hasBeginRendering = true;
134                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
135                         }
136
137                         if (ToolTip.Length > 0){
138                                 hasBeginRendering = true;
139                                 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
140                         }
141
142                         if (Attributes.Count > 0){
143                                 string val = Attributes ["value"];
144                                 Attributes.Remove ("value");
145                                 if (Attributes.Count > 0){
146                                         hasBeginRendering = true;
147                                         Attributes.AddAttributes (writer);
148                                 }
149
150                                 if (val != null)
151                                         Attributes ["value"] = val;
152                         }
153
154                         if (hasBeginRendering)
155                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
156
157                         if (Text.Length > 0){
158                                 TextAlign ta = TextAlign;
159                                 if(ta == TextAlign.Right)
160                                         RenderInputTag (writer, ClientID);
161                                 writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
162                                 writer.RenderBeginTag (HtmlTextWriterTag.Label);
163                                 writer.Write (Text);
164                                 writer.RenderEndTag ();
165                                 if(ta == TextAlign.Left)
166                                         RenderInputTag (writer, ClientID);
167                         }
168                         else
169                                 RenderInputTag (writer, ClientID);
170
171                         if (hasBeginRendering)
172                                 writer.RenderEndTag ();
173                 }
174                 
175                 internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
176                 {
177                         writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
178                         writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
179                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
180                         if (Checked)
181                                 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
182
183                         if (AutoPostBack){
184                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
185                                                      Page.GetPostBackClientEvent (this, String.Empty));
186                                 writer.AddAttribute ("language", "javascript");
187                         }
188
189                         if (AccessKey.Length > 0)
190                                 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
191
192                         if (TabIndex != 0)
193                                 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
194                                                      TabIndex.ToString (NumberFormatInfo.InvariantInfo));
195
196                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
197                         writer.RenderEndTag ();
198                 }
199                 
200                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
201                 {
202                         string postedVal = postCollection [postDataKey];
203                         bool   postChecked = false;
204                         if(postedVal != null)
205                                 postChecked = postedVal.Length > 0;
206                         Checked = postChecked;
207                         return (postChecked == false);
208                 }
209                 
210                 void IPostBackDataHandler.RaisePostDataChangedEvent()
211                 {
212                         OnCheckedChanged (EventArgs.Empty);
213                 }
214         }
215 }