Merge pull request #2916 from ludovic-henry/fix-40306
[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-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 using System.Reflection;
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 BoundField : DataControlField
43         {
44                 public static readonly string ThisExpression = "!";
45                 
46                 //PropertyDescriptor boundProperty;
47
48                 [DefaultValueAttribute (false)]
49                 [WebSysDescription ("")]
50                 [WebCategoryAttribute ("Behavior")]
51                 public virtual bool ApplyFormatInEditMode {
52                         get { return ViewState.GetBool ("ApplyFormatInEditMode", false); }
53                         set { ViewState ["ApplyFormatInEditMode"] = value; }
54                 }
55                 
56                 [DefaultValueAttribute (true)]
57                 [WebSysDescription ("")]
58                 [WebCategoryAttribute ("Behavior")]
59                 public virtual bool ConvertEmptyStringToNull {
60                         get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
61                         set {
62                                 ViewState ["ConvertEmptyStringToNull"] = value;
63                                 OnFieldChanged ();
64                         }
65                 }
66
67                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
68                 [WebSysDescription ("")]
69                 [WebCategoryAttribute ("Data")]
70                 [DefaultValueAttribute ("")]
71                 public virtual string DataField {
72                         get { return ViewState.GetString ("DataField", String.Empty); }
73                         set {
74                                 ViewState ["DataField"] = value;
75                                 OnFieldChanged ();
76                         }
77                 }
78
79                 [DefaultValueAttribute ("")]
80                 [WebSysDescription ("")]
81                 [WebCategoryAttribute ("Data")]
82                 public virtual string DataFormatString {
83                         get { return ViewState.GetString ("DataFormatString", String.Empty); }
84                         set {
85                                 ViewState ["DataFormatString"] = value;
86                                 OnFieldChanged ();
87                         }
88                 }
89
90                 [WebSysDescription ("")]
91                 [WebCategoryAttribute ("Appearance")]
92                 public override string HeaderText {
93                         get { return ViewState.GetString ("HeaderText", String.Empty); }
94                         set {
95                                 ViewState["HeaderText"] = value;
96                                 OnFieldChanged ();
97                         }
98                 }
99
100                 [DefaultValueAttribute ("")]
101                 [WebCategoryAttribute ("Behavior")]
102                 public virtual string NullDisplayText {
103                         get { return ViewState.GetString ("NullDisplayText", String.Empty); }
104                         set {
105                                 ViewState ["NullDisplayText"] = value;
106                                 OnFieldChanged ();
107                         }
108                 }
109
110                 [DefaultValueAttribute (false)]
111                 [WebSysDescription ("")]
112                 [WebCategoryAttribute ("Behavior")]
113                 public virtual bool ReadOnly {
114                         get { return ViewState.GetBool ("ReadOnly", false); }
115                         set { 
116                                 ViewState ["ReadOnly"] = value;
117                                 OnFieldChanged ();
118                         }
119                 }
120
121                 [DefaultValueAttribute (true)]
122                 [WebSysDescription ("")]
123                 [WebCategoryAttribute ("HtmlEncode")]
124                 public virtual bool HtmlEncode {
125                         get { return ViewState.GetBool ("HtmlEncode", true); }
126                         set { 
127                                 ViewState ["HtmlEncode"] = value;
128                                 OnFieldChanged ();
129                         }
130                 }
131
132                 [DefaultValue (true)]
133                 public virtual bool HtmlEncodeFormatString {
134                         get { return ViewState.GetBool ("HtmlEncodeFormatString", true); }
135                         set {
136                                 ViewState ["HtmlEncodeFormatString"] = value;
137                                 OnFieldChanged ();
138                         }
139                 }
140                 
141                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
142                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
143                 {
144                         bool editable = IsEditable (rowState);
145                         if (editable) {
146                                 if (cell.Controls.Count > 0) {
147                                         TextBox box = (TextBox) cell.Controls [0];
148                                         dictionary [DataField] = box.Text;
149                                 }
150                         } else if (includeReadOnly)
151                                 dictionary [DataField] = cell.Text;
152                 }
153
154                 public override bool Initialize (bool enableSorting, Control control)
155                 {
156                         return base.Initialize (enableSorting, control);
157                 }
158
159                 public override void InitializeCell (DataControlFieldCell cell,
160                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
161                 {
162                         base.InitializeCell (cell, cellType, rowState, rowIndex);
163                         if (cellType == DataControlCellType.DataCell) {
164                                 InitializeDataCell (cell, rowState);
165                                 if ((rowState & DataControlRowState.Insert) == 0)
166                                         cell.DataBinding += new EventHandler (OnDataBindField);
167                         }
168                 }
169                 
170                 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
171                 {
172                         bool editable = IsEditable (rowState);
173                         if (editable) {
174                                 TextBox box = new TextBox ();
175                                 cell.Controls.Add (box);
176                                 box.ToolTip = HeaderText;
177                         }
178                 }
179
180                 internal bool IsEditable (DataControlRowState rowState)
181                 {
182                         return ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) || ((rowState & DataControlRowState.Insert) != 0 && InsertVisible);
183                 }
184
185                 protected virtual bool SupportsHtmlEncode {
186                         get { return true; }
187                 }
188                 
189                 protected virtual string FormatDataValue (object value, bool encode)
190                 {
191                         string res;
192                         bool htmlEncodeFormatString = HtmlEncodeFormatString;
193                         string stringValue = (value != null) ? value.ToString () : String.Empty;
194                         if (value == null || (stringValue.Length == 0 && ConvertEmptyStringToNull)) {
195                                 if (NullDisplayText.Length == 0) {
196                                         encode = false;
197                                         res = " ";
198                                 } else
199                                         res = NullDisplayText;
200                         } else {
201                                 string format = DataFormatString;
202                                 if (!String.IsNullOrEmpty (format)) {
203                                         if (!encode || htmlEncodeFormatString)
204                                                 res = String.Format (format, value);
205                                         else
206                                                 res = String.Format (format, encode ? HttpUtility.HtmlEncode (stringValue) : stringValue);
207                                 } else
208                                         res = stringValue;
209                         }
210                         
211                         if (encode && htmlEncodeFormatString)
212                                 return HttpUtility.HtmlEncode (res);
213                         else
214                                 return res;
215                 }
216                 
217                 protected virtual object GetValue (Control controlContainer)
218                 {
219                         if (DesignMode)
220                                 return GetDesignTimeValue ();
221                         else
222                                 return GetBoundValue (controlContainer);
223                 }
224                 
225                 protected virtual object GetDesignTimeValue ()
226                 {
227                         return "Databound";
228                 }
229
230                 object GetBoundValue (Control controlContainer)
231                 {
232                         object dataItem = DataBinder.GetDataItem (controlContainer);
233                         if (dataItem == null)
234                                 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
235
236                         if (DataField == ThisExpression)
237                                 return dataItem;
238                         else if (DataField == string.Empty)
239                                 return null;
240
241                         return DataBinder.GetPropertyValue (dataItem, DataField);
242                 }
243                 
244                 protected override void LoadViewState (object state)
245                 {
246                         // Why override?
247                         base.LoadViewState (state);
248                 }
249                 
250                 protected virtual void OnDataBindField (object sender, EventArgs e)
251                 {
252                         Control container = (Control) sender;
253                         Control controlContainer = container.BindingContainer;
254                         if (!(controlContainer is INamingContainer))
255                                 throw new HttpException ("A DataControlField must be within an INamingContainer.");
256                         object val = GetValue (controlContainer);
257                         TextBox box = sender as TextBox;
258
259                         if (box == null) {
260                                 var cell = sender as DataControlFieldCell;
261                                 if (cell != null) {
262                                         ControlCollection controls = cell.Controls;
263                                         int ccount = controls != null ? controls.Count : 0;
264                                         if (ccount == 1)
265                                                 box = controls [0] as TextBox;
266                                         if (box == null) {
267                                                 cell.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
268                                                 return;
269                                         }
270                                 }
271                         }
272
273                         if (box == null)
274                                 throw new HttpException ("Bound field " + DataField + " contains a control that isn't a TextBox.  Override OnDataBindField to inherit from BoundField and add different controls.");
275                         if (ApplyFormatInEditMode)
276                                 box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
277                         else
278                                 box.Text = val != null ? val.ToString() : NullDisplayText;
279                 }
280                 
281                 protected override DataControlField CreateField ()
282                 {
283                         return new BoundField ();
284                 }
285                 
286                 protected override void CopyProperties (DataControlField newField)
287                 {
288                         base.CopyProperties (newField);
289                         BoundField field = (BoundField) newField;
290                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
291                         field.DataField = DataField;
292                         field.DataFormatString = DataFormatString;
293                         field.NullDisplayText = NullDisplayText;
294                         field.ReadOnly = ReadOnly;
295                         field.HtmlEncode = HtmlEncode;
296                 }
297
298                 // MSDN: The ValidateSupportsCallback method is a helper method used to determine 
299                 // whether the controls contained in a BoundField object support callbacks. 
300                 // This method has been implemented as an empty method (a method that does not contain 
301                 // any code) to indicate that callbacks are supported.
302                 public override void ValidateSupportsCallback ()
303                 {
304                 }
305
306         }
307 }
308