New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundField.cs
1 //
2 // System.Web.UI.WebControls.BoundField.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 using System.Reflection;
38
39 namespace System.Web.UI.WebControls {
40
41         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class BoundField : DataControlField
44         {
45                 public static readonly string ThisExpression = "!";
46                 
47                 PropertyDescriptor boundProperty;
48
49                 [DefaultValueAttribute (false)]
50                 [WebSysDescription ("")]
51                 [WebCategoryAttribute ("Behavior")]
52                 public virtual bool ApplyFormatInEditMode {
53                         get {
54                                 return ViewState.GetBool ("ApplyFormatInEditMode", false);
55                         }
56                         set {
57                                 ViewState ["ApplyFormatInEditMode"] = value;
58                         }
59                 }
60                 
61                 [DefaultValueAttribute (true)]
62                 [WebSysDescription ("")]
63                 [WebCategoryAttribute ("Behavior")]
64                 public virtual bool ConvertEmptyStringToNull {
65                         get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
66                         set {
67                                 ViewState ["ConvertEmptyStringToNull"] = value;
68                                 OnFieldChanged ();
69                         }
70                 }
71
72                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
73                 [WebSysDescription ("")]
74                 [WebCategoryAttribute ("Data")]
75                 [DefaultValueAttribute ("")]
76                 public virtual string DataField {
77                         get { return ViewState.GetString ("DataField", ""); }
78                         set {
79                                 ViewState ["DataField"] = value;
80                                 OnFieldChanged ();
81                         }
82                 }
83
84                 [DefaultValueAttribute ("")]
85                 [WebSysDescription ("")]
86                 [WebCategoryAttribute ("Data")]
87                 public virtual string DataFormatString {
88                         get { return ViewState.GetString ("DataFormatString", ""); }
89                         set {
90                                 ViewState ["DataFormatString"] = value;
91                                 OnFieldChanged ();
92                         }
93                 }
94
95                 [MonoTODO]
96                 [WebSysDescription ("")]
97                 [WebCategoryAttribute ("Appearance")]
98                 public override string HeaderText {
99                         get { return ViewState.GetString ("HeaderText", "");
100                         }
101                         set {
102                                 ViewState["HeaderText"] = value;
103                                 OnFieldChanged ();
104                         }
105                 }
106
107                 [DefaultValueAttribute ("")]
108                 [WebCategoryAttribute ("Behavior")]
109                 public virtual string NullDisplayText {
110                         get { return ViewState.GetString ("NullDisplayText", ""); }
111                         set {
112                                 ViewState ["NullDisplayText"] = value;
113                                 OnFieldChanged ();
114                         }
115                 }
116
117                 [DefaultValueAttribute (false)]
118                 [WebSysDescription ("")]
119                 [WebCategoryAttribute ("Behavior")]
120                 public virtual bool ReadOnly {
121                         get { return ViewState.GetBool ("ReadOnly", false); }
122                         set { 
123                                 ViewState ["ReadOnly"] = value;
124                                 OnFieldChanged ();
125                         }
126                 }
127
128                 [DefaultValueAttribute (true)]
129                 [WebSysDescription ("")]
130                 [WebCategoryAttribute ("HtmlEncode")]
131                 public virtual bool HtmlEncode {
132                         get { return ViewState.GetBool ("HtmlEncode", true); }
133                         set { 
134                                 ViewState ["HtmlEncode"] = value;
135                                 OnFieldChanged ();
136                         }
137                 }
138
139                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
140                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
141                 {
142                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
143                         if (editable && !ReadOnly) {
144                                 if (cell.Controls.Count > 0) {
145                                         TextBox box = (TextBox) cell.Controls [0];
146                                         dictionary [DataField] = box.Text;
147                                 }
148                         } else if (includeReadOnly) {
149                                 dictionary [DataField] = cell.Text;
150                         }
151                 }
152
153                 [MonoTODO]
154                 public override bool Initialize (bool enableSorting, 
155                                                  Control control)
156                 {
157                         return base.Initialize (enableSorting, control);
158                 }
159
160                 public override void InitializeCell (DataControlFieldCell cell,
161                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
162                 {
163                         base.InitializeCell (cell, cellType, rowState, rowIndex);
164                         if (cellType == DataControlCellType.DataCell) {
165                                 InitializeDataCell (cell, rowState);
166                                 if ((rowState & DataControlRowState.Insert) == 0)
167                                         cell.DataBinding += new EventHandler (OnDataBindField);
168                         }
169                 }
170                 
171                 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
172                 {
173                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
174                         if (editable && !ReadOnly) {
175                                 TextBox box = new TextBox ();
176                                 box.ID = cell.ClientID;
177                                 cell.Controls.Add (box);
178                         }
179                 }
180                 
181                 protected virtual bool SupportsHtmlEncode {
182                         get { return true; }
183                 }
184                 
185                 protected virtual string FormatDataValue (object value, bool encode)
186                 {
187                         string res;
188                         if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
189                                 if (NullDisplayText.Length == 0) {
190                                         encode = false;
191                                         res = " ";
192                                 }
193                                 else
194                                         res = NullDisplayText;
195                         }
196                         else if (DataFormatString.Length > 0)
197                                 res = string.Format (DataFormatString, value);
198                         else
199                                 res = value.ToString ();
200                                 
201                         if (encode) return HttpUtility.HtmlEncode (res);
202                         else return res;
203                 }
204                 
205                 protected virtual object GetValue (Control controlContainer)
206                 {
207                         if (DesignMode)
208                                 return GetDesignTimeValue ();
209                         else
210                                 return GetBoundValue (controlContainer);
211                 }
212                 
213                 protected virtual object GetDesignTimeValue ()
214                 {
215                         return GetBoundValue (Control);
216                 }
217
218                 object GetBoundValue (Control controlContainer)
219                 {
220                         object dataItem = DataBinder.GetDataItem (controlContainer);
221                         if (dataItem == null)
222                                 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
223
224                         if (DataField == ThisExpression)
225                                 return dataItem.ToString ();
226                         else if (DataField == string.Empty)
227                                 return null;
228
229                         return DataBinder.GetPropertyValue (dataItem, DataField);
230                 }
231                 
232                 protected virtual void OnDataBindField (object sender, EventArgs e)
233                 {
234                         Control cell = (Control) sender;
235                         Control controlContainer = cell.BindingContainer;
236                         if (!(controlContainer is INamingContainer))
237                                 throw new HttpException ("A DataControlField must be within an INamingContainer.");
238                         object val = GetValue (controlContainer);
239
240                         if (cell.Controls.Count > 0) {
241                                 TextBox box = (TextBox) cell.Controls [0];
242                                 if (ApplyFormatInEditMode)
243                                         box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
244                                 else
245                                         box.Text = val != null ? val.ToString() : NullDisplayText;
246                         }
247                         else
248                                 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
249                 }
250                 
251                 protected override DataControlField CreateField ()
252                 {
253                         return new BoundField ();
254                 }
255                 
256                 protected override void CopyProperties (DataControlField newField)
257                 {
258                         base.CopyProperties (newField);
259                         BoundField field = (BoundField) newField;
260                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
261                         field.DataField = DataField;
262                         field.DataFormatString = DataFormatString;
263                         field.NullDisplayText = NullDisplayText;
264                         field.ReadOnly = ReadOnly;
265                         field.HtmlEncode = HtmlEncode;
266                 }
267
268                 [MonoTODO]
269                 public override void ValidateSupportsCallback ()
270                 {
271                         throw new NotImplementedException ();
272                 }
273
274         }
275 }
276 #endif