Merge pull request #396 from directhex/master
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxField.cs
1 //
2 // System.Web.UI.WebControls.CheckBoxField.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Web.UI;
34 using System.ComponentModel;
35 using System.Security.Permissions;
36
37 namespace System.Web.UI.WebControls
38 {
39         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public class CheckBoxField : BoundField
42         {
43                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
44                 [BrowsableAttribute (false)]
45                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
46                 public override bool ApplyFormatInEditMode {
47                         get { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
48                         set { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
49                 }
50
51                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
52                 [BrowsableAttribute (false)]
53                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
54                 public override bool ConvertEmptyStringToNull {
55                         get { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); } 
56                         set { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); } 
57                 }
58
59                 [TypeConverter ("System.Web.UI.Design.DataSourceBooleanViewSchemaConverter, " + Consts.AssemblySystem_Design)]
60                 public override string DataField {
61                         get { return base.DataField; }
62                         set { base.DataField = value; }
63                 }
64
65                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
66                 [BrowsableAttribute (false)]
67                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
68                 public override string DataFormatString {
69                         get { throw GetNotSupportedPropException ("DataFormatString"); } 
70                         set { throw GetNotSupportedPropException ("DataFormatString"); } 
71                 }
72                 
73                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
74                 [BrowsableAttribute (false)]
75                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
76                 public override bool HtmlEncode {
77                         get { throw GetNotSupportedPropException ("HtmlEncode"); } 
78                         set { throw GetNotSupportedPropException ("HtmlEncode"); } 
79                 }
80
81                 [Browsable (false)]
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83                 [EditorBrowsable (EditorBrowsableState.Never)]
84                 public override bool HtmlEncodeFormatString {
85                         get { return base.HtmlEncodeFormatString; }
86                         set { base.HtmlEncodeFormatString = value; }
87                 }
88                 
89                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
90                 [BrowsableAttribute (false)]
91                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
92                 public override string NullDisplayText {
93                         get { throw GetNotSupportedPropException ("NullDisplayText"); } 
94                         set { throw GetNotSupportedPropException ("NullDisplayText"); } 
95                 }
96                 
97                 protected override bool SupportsHtmlEncode {
98                         get { return false; }
99                 }
100                 
101                 [LocalizableAttribute (true)]
102                 [DefaultValueAttribute ("")]
103                 [WebSysDescription ("")]
104                 [WebCategoryAttribute ("Appearance")]
105                 public virtual string Text {
106                         get { return ViewState.GetString ("Text", String.Empty); }
107                         set {
108                                 ViewState ["Text"] = value;
109                                 OnFieldChanged ();
110                         }
111                 }
112                 
113                 protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
114                 {
115                         bool editable = IsEditable (rowState);
116                         CheckBox box = new CheckBox ();
117                         box.Enabled = editable;
118                         if (editable)
119                                 box.ToolTip = HeaderText;
120                         box.Text = Text;
121                         cell.Controls.Add (box);
122                 }
123                 
124                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
125                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
126                 {
127                         bool editable = IsEditable (rowState);
128                         if (editable || includeReadOnly) {
129                                 CheckBox box = (CheckBox) cell.Controls [0];
130                                 dictionary [DataField] = box.Checked;
131                         }
132                 }
133                 
134                 protected override void OnDataBindField (object sender, EventArgs e)
135                 {
136                         try {
137                                 Control container = (Control) sender;
138                                 object val = GetValue (container.NamingContainer);
139                                 CheckBox box = sender as CheckBox;
140                                 if (box == null) {
141                                         DataControlFieldCell cell = sender as DataControlFieldCell;
142                                         if (cell != null) {
143                                                 ControlCollection controls = cell.Controls;
144                                                 int ccount = controls != null ? controls.Count : 0;
145                                                 if (ccount == 1)
146                                                         box = controls [0] as CheckBox;
147                                                 if (box == null)
148                                                         return;
149                                         }
150                                 }
151                                 
152                                 if (box == null)
153                                         throw new HttpException ("CheckBox field '" + DataField + "' contains a control that isn't a CheckBox.  Override OnDataBindField to inherit from CheckBoxField and add different controls.");
154                                 
155                                 if (val != null && val != DBNull.Value)
156                                         box.Checked = (bool) val;
157                                 else {
158                                         if (string.IsNullOrEmpty (DataField)) {
159                                                 box.Visible = false;
160                                                 return;
161                                         }
162                                 }
163                                 
164                                 if (!box.Visible)
165                                         box.Visible = true;
166                         } catch (HttpException) {
167                                 throw;
168                         } catch (Exception ex) {
169                                 throw new HttpException (ex.Message, ex);
170                         }
171                 }
172                 
173                 protected override object GetDesignTimeValue ()
174                 {
175                         return true;
176                 }
177                 
178                 protected override DataControlField CreateField ()
179                 {
180                         return new CheckBoxField ();
181                 }
182                 
183                 protected override void CopyProperties (DataControlField newField)
184                 {
185                         CheckBoxField field = (CheckBoxField) newField;
186                         field.DataField = DataField;
187                         field.ReadOnly = ReadOnly;
188                         field.Text = Text;
189                 }
190
191                 public override void ValidateSupportsCallback ()
192                 {
193                         // why override?
194                 }
195         }
196 }
197