bug fixes found by Test/System.Web.UI.WebControls/BoundFieldTest
[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                                 res = NullDisplayText;
190                         else if (DataFormatString.Length > 0)
191                                 res = string.Format (DataFormatString, value);
192                         else
193                                 res = value.ToString ();
194                                 
195                         if (encode) return HttpUtility.HtmlEncode (res);
196                         else return res;
197                 }
198                 
199                 protected virtual object GetValue (Control controlContainer)
200                 {
201                         if (DesignMode)
202                                 return GetDesignTimeValue ();
203                         else
204                                 return GetBoundValue (controlContainer);
205                 }
206                 
207                 protected virtual object GetDesignTimeValue ()
208                 {
209                         return GetBoundValue (Control);
210                 }
211
212                 object GetBoundValue (Control controlContainer)
213                 {
214                         object dataItem = DataBinder.GetDataItem (controlContainer);
215                         if (dataItem == null)
216                                 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
217
218                         if (DataField == ThisExpression)
219                                 return dataItem.ToString ();
220
221                         return DataBinder.GetPropertyValue (dataItem, DataField);
222                 }
223                 
224                 protected virtual void OnDataBindField (object sender, EventArgs e)
225                 {
226                         Control cell = (Control) sender;
227                         Control controlContainer = cell.BindingContainer;
228                         if (!(controlContainer is INamingContainer))
229                                 throw new HttpException ("A DataControlField must be within an INamingContainer.");
230                         object val = GetValue (controlContainer);
231
232                         if (cell.Controls.Count > 0) {
233                                 TextBox box = (TextBox) cell.Controls [0];
234                                 if (ApplyFormatInEditMode)
235                                         box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
236                                 else
237                                         box.Text = val != null ? val.ToString() : NullDisplayText;
238                         }
239                         else
240                                 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
241                 }
242                 
243                 protected override DataControlField CreateField ()
244                 {
245                         return new BoundField ();
246                 }
247                 
248                 protected override void CopyProperties (DataControlField newField)
249                 {
250                         base.CopyProperties (newField);
251                         BoundField field = (BoundField) newField;
252                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
253                         field.DataField = DataField;
254                         field.DataFormatString = DataFormatString;
255                         field.NullDisplayText = NullDisplayText;
256                         field.ReadOnly = ReadOnly;
257                         field.HtmlEncode = HtmlEncode;
258                 }
259
260                 [MonoTODO]
261                 public override void ValidateSupportsCallback ()
262                 {
263                         throw new NotImplementedException ();
264                 }
265
266         }
267 }
268 #endif