merge from trunk revisions 58933, 58935, 58936
[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                 [MonoTODO]
50                 [DefaultValueAttribute (false)]
51                 [WebSysDescription ("")]
52                 [WebCategoryAttribute ("Behavior")]
53                 public virtual bool ApplyFormatInEditMode {
54                         get {
55                                 throw new NotImplementedException ();
56                         }
57                         set {
58                                 throw new NotImplementedException ();
59                         }
60                 }
61                 
62                 [DefaultValueAttribute (true)]
63                 [WebSysDescription ("")]
64                 [WebCategoryAttribute ("Behavior")]
65                 public virtual bool ConvertEmptyStringToNull {
66                         get {
67                                 object ob = ViewState ["ConvertEmptyStringToNull"];
68                                 if (ob != null) return (bool) ob;
69                                 return true;
70                         }
71                         set {
72                                 ViewState ["ConvertEmptyStringToNull"] = value;
73                                 OnFieldChanged ();
74                         }
75                 }
76
77                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
78                 [WebSysDescription ("")]
79                 [WebCategoryAttribute ("Data")]
80                 [DefaultValueAttribute ("")]
81                 public virtual string DataField {
82                         get {
83                                 object ob = ViewState ["DataField"];
84                                 if (ob != null) return (string) ob;
85                                 return "";
86                         }
87                         set {
88                                 ViewState ["DataField"] = value;
89                                 OnFieldChanged ();
90                         }
91                 }
92
93                 [DefaultValueAttribute ("")]
94                 [WebSysDescription ("")]
95                 [WebCategoryAttribute ("Data")]
96                 public virtual string DataFormatString {
97                         get {
98                                 object ob = ViewState ["DataFormatString"];
99                                 if (ob != null) return (string) ob;
100                                 return "";
101                         }
102                         set {
103                                 ViewState ["DataFormatString"] = value;
104                                 OnFieldChanged ();
105                         }
106                 }
107
108                 [MonoTODO]
109                 [WebSysDescription ("")]
110                 [WebCategoryAttribute ("Appearance")]
111                 public virtual string HeaderText {
112                         get {
113                                 throw new NotImplementedException ();
114                         }
115                         set {
116                                 throw new NotImplementedException ();
117                         }
118                 }
119
120                 [DefaultValueAttribute ("")]
121                 [WebCategoryAttribute ("Behavior")]
122                 public virtual string NullDisplayText {
123                         get {
124                                 object ob = ViewState ["NullDisplayText"];
125                                 if (ob != null) return (string) ob;
126                                 return "";
127                         }
128                         set {
129                                 ViewState ["NullDisplayText"] = value;
130                                 OnFieldChanged ();
131                         }
132                 }
133
134                 [DefaultValueAttribute (false)]
135                 [WebSysDescription ("")]
136                 [WebCategoryAttribute ("Behavior")]
137                 public bool ReadOnly {
138                         get {
139                                 object val = ViewState ["ReadOnly"];
140                                 return val != null ? (bool) val : false;
141                         }
142                         set { 
143                                 ViewState ["ReadOnly"] = value;
144                                 OnFieldChanged ();
145                         }
146                 }
147
148                 [DefaultValueAttribute (true)]
149                 [WebSysDescription ("")]
150                 [WebCategoryAttribute ("HtmlEncode")]
151                 public virtual bool HtmlEncode {
152                         get {
153                                 object val = ViewState ["HtmlEncode"];
154                                 return val != null ? (bool) val : true;
155                         }
156                         set { 
157                                 ViewState ["HtmlEncode"] = true;
158                                 OnFieldChanged ();
159                         }
160                 }
161
162                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
163                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
164                 {
165                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
166                         if (editable && !ReadOnly) {
167                                 if (cell.Controls.Count > 0) {
168                                         TextBox box = (TextBox) cell.Controls [0];
169                                         dictionary [DataField] = box.Text;
170                                 }
171                         } else if (includeReadOnly) {
172                                 dictionary [DataField] = cell.Text;
173                         }
174                 }
175
176                 [MonoTODO]
177                 public override bool Initialize (bool enableSorting, 
178                                                  Control control)
179                 {
180                         return base.Initialize (enableSorting, control);
181                 }
182
183                 public override void InitializeCell (DataControlFieldCell cell,
184                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
185                 {
186                         base.InitializeCell (cell, cellType, rowState, rowIndex);
187                         if (cellType == DataControlCellType.DataCell) {
188                                 InitializeDataCell (cell, rowState);
189                                 if ((rowState & DataControlRowState.Insert) == 0)
190                                         cell.DataBinding += new EventHandler (OnDataBindField);
191                         }
192                 }
193                 
194                 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
195                 {
196                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
197                         if (editable && !ReadOnly) {
198                                 TextBox box = new TextBox ();
199                                 cell.Controls.Add (box);
200                         }
201                 }
202                 
203                 protected virtual bool SupportsHtmlEncode {
204                         get { return true; }
205                 }
206                 
207                 protected virtual string FormatDataValue (object value, bool encode)
208                 {
209                         string res;
210                         if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
211                                 res = NullDisplayText;
212                         else if (DataFormatString.Length > 0)
213                                 res = string.Format (DataFormatString, value);
214                         else
215                                 res = value.ToString ();
216                                 
217                         if (encode) return HttpUtility.HtmlEncode (res);
218                         else return res;
219                 }
220                 
221                 protected virtual object GetValue (Control controlContainer)
222                 {
223                         if (DesignMode)
224                                 return GetDesignTimeValue ();
225                         else
226                                 return GetBoundValue (controlContainer);
227                 }
228                 
229                 protected virtual object GetDesignTimeValue ()
230                 {
231                         return GetBoundValue (Control);
232                 }
233                 
234                 object GetBoundValue (Control controlContainer)
235                 {
236                         if (DataField == ThisExpression)
237                                 return controlContainer.ToString ();
238                         else {
239                                 IDataItemContainer dic = (IDataItemContainer) controlContainer;
240                                 if (boundProperty == null) {
241                                         boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
242                                         if (boundProperty == null)
243                                                 new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
244                                 }
245                                 return boundProperty.GetValue (dic.DataItem);
246                         }
247                 }
248                 
249                 protected virtual void OnDataBindField (object sender, EventArgs e)
250                 {
251                         DataControlFieldCell cell = (DataControlFieldCell) sender;
252                         if (cell.Controls.Count > 0) {
253                                 TextBox box = (TextBox) cell.Controls [0];
254                                 object val = GetValue (cell.BindingContainer);
255                                 box.Text = val != null ? val.ToString() : "";
256                         }
257                         else
258                                 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
259                 }
260                 
261                 protected override DataControlField CreateField ()
262                 {
263                         return new BoundField ();
264                 }
265                 
266                 protected override void CopyProperties (DataControlField newField)
267                 {
268                         base.CopyProperties (newField);
269                         BoundField field = (BoundField) newField;
270                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
271                         field.DataField = DataField;
272                         field.DataFormatString = DataFormatString;
273                         field.NullDisplayText = NullDisplayText;
274                         field.ReadOnly = ReadOnly;
275                         field.HtmlEncode = HtmlEncode;
276                 }
277
278                 [MonoTODO]
279                 public override void ValidateSupportsCallback ()
280                 {
281                         throw new NotImplementedException ();
282                 }
283
284         }
285 }
286 #endif