New test.
[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 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 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Web.UI;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37
38 namespace System.Web.UI.WebControls {
39
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public class CheckBoxField : BoundField
43         {
44                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
45                 [BrowsableAttribute (false)]
46                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
47                 public override bool ApplyFormatInEditMode {
48                         get { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
49                         set { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
50                 }
51
52             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
53             [BrowsableAttribute (false)]
54             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
55                 public override bool ConvertEmptyStringToNull {
56                         get { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); } 
57                         set { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); } 
58                 }
59                 
60             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
61             [BrowsableAttribute (false)]
62             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
63                 public override string DataFormatString {
64                         get { throw GetNotSupportedPropException ("DataFormatString"); } 
65                         set { throw GetNotSupportedPropException ("DataFormatString"); } 
66                 }
67                 
68             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
69             [BrowsableAttribute (false)]
70             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
71                 public override bool HtmlEncode {
72                         get { throw GetNotSupportedPropException ("HtmlEncode"); } 
73                         set { throw GetNotSupportedPropException ("HtmlEncode"); } 
74                 }
75                 
76             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
77             [BrowsableAttribute (false)]
78             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
79                 public override string NullDisplayText {
80                         get { throw GetNotSupportedPropException ("NullDisplayText"); } 
81                         set { throw GetNotSupportedPropException ("NullDisplayText"); } 
82                 }
83                 
84                 protected override bool SupportsHtmlEncode {
85                         get { return false; }
86                 }
87                 
88                 [LocalizableAttribute (true)]
89                 [DefaultValueAttribute ("")]
90                 [WebSysDescription ("")]
91                 [WebCategoryAttribute ("Appearance")]
92                 public virtual string Text {
93                         get { return ViewState.GetString ("Text", ""); }
94                         set {
95                                 ViewState ["Text"] = value;
96                                 OnFieldChanged ();
97                         }
98                 }
99                 
100                 protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
101                 {
102                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
103                         CheckBox box = new CheckBox ();
104                         box.Enabled = editable && !ReadOnly;\r
105                         if (editable)
106                                 box.ToolTip = HeaderText;
107                         box.Text = Text;
108                         cell.Controls.Add (box);
109                 }
110                 
111                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
112                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
113                 {
114                         CheckBox box = (CheckBox) cell.Controls [0];
115                         dictionary [DataField] = box.Checked;
116                 }
117                 
118                 protected override void OnDataBindField (object sender, EventArgs e)
119                 {
120                         try {
121                                 DataControlFieldCell cell = (DataControlFieldCell) sender;
122                                 CheckBox box = (CheckBox) cell.Controls [0];
123                                 object val = GetValue (cell.BindingContainer);
124                                 if (val != null) {
125                                         box.Checked = (bool) val;
126                                         if (!box.Visible)
127                                                 box.Visible = true;
128                                 }
129                                 else
130                                         box.Visible = false;
131                         }
132                         catch (HttpException) {
133                                 throw;
134                         }
135                         catch (Exception ex) {
136                                 throw new HttpException (ex.Message, ex);
137                         }
138                 }
139                 
140                 protected override object GetDesignTimeValue ()
141                 {
142                         return true;
143                 }
144                 
145                 protected override DataControlField CreateField ()
146                 {
147                         return new CheckBoxField ();
148                 }
149                 
150                 protected override void CopyProperties (DataControlField newField)
151                 {
152                         CheckBoxField field = (CheckBoxField) newField;
153                         field.DataField = DataField;
154                         field.ReadOnly = ReadOnly;
155                         field.Text = Text;
156                 }
157         }
158 }
159 #endif