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                 [WebSysDescription ("")]
96                 [WebCategoryAttribute ("Appearance")]
97                 public override string HeaderText {
98                         get { return ViewState.GetString ("HeaderText", "");
99                         }
100                         set {
101                                 ViewState["HeaderText"] = value;
102                                 OnFieldChanged ();
103                         }
104                 }
105
106                 [DefaultValueAttribute ("")]
107                 [WebCategoryAttribute ("Behavior")]
108                 public virtual string NullDisplayText {
109                         get { return ViewState.GetString ("NullDisplayText", ""); }
110                         set {
111                                 ViewState ["NullDisplayText"] = value;
112                                 OnFieldChanged ();
113                         }
114                 }
115
116                 [DefaultValueAttribute (false)]
117                 [WebSysDescription ("")]
118                 [WebCategoryAttribute ("Behavior")]
119                 public virtual bool ReadOnly {
120                         get { return ViewState.GetBool ("ReadOnly", false); }
121                         set { 
122                                 ViewState ["ReadOnly"] = value;
123                                 OnFieldChanged ();
124                         }
125                 }
126
127                 [DefaultValueAttribute (true)]
128                 [WebSysDescription ("")]
129                 [WebCategoryAttribute ("HtmlEncode")]
130                 public virtual bool HtmlEncode {
131                         get { return ViewState.GetBool ("HtmlEncode", true); }
132                         set { 
133                                 ViewState ["HtmlEncode"] = value;
134                                 OnFieldChanged ();
135                         }
136                 }
137
138                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
139                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
140                 {
141                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
142                         if (editable && !ReadOnly) {
143                                 if (cell.Controls.Count > 0) {
144                                         TextBox box = (TextBox) cell.Controls [0];
145                                         dictionary [DataField] = box.Text;
146                                 }
147                         } else if (includeReadOnly) {
148                                 dictionary [DataField] = cell.Text;
149                         }
150                 }
151
152                 public override bool Initialize (bool enableSorting, 
153                                                  Control control)
154                 {
155                         return base.Initialize (enableSorting, control);
156                 }
157
158                 public override void InitializeCell (DataControlFieldCell cell,
159                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
160                 {
161                         base.InitializeCell (cell, cellType, rowState, rowIndex);
162                         if (cellType == DataControlCellType.DataCell) {
163                                 InitializeDataCell (cell, rowState);
164                                 if ((rowState & DataControlRowState.Insert) == 0)
165                                         cell.DataBinding += new EventHandler (OnDataBindField);
166                         }
167                 }
168                 
169                 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
170                 {
171                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
172                         if (editable && !ReadOnly) {
173                                 TextBox box = new TextBox ();
174                                 box.ID = cell.ClientID;
175                                 cell.Controls.Add (box);
176                         }
177                 }
178                 
179                 protected virtual bool SupportsHtmlEncode {
180                         get { return true; }
181                 }
182                 
183                 protected virtual string FormatDataValue (object value, bool encode)
184                 {
185                         string res;
186                         if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
187                                 if (NullDisplayText.Length == 0) {
188                                         encode = false;
189                                         res = " ";
190                                 }
191                                 else
192                                         res = NullDisplayText;
193                         }
194                         else if (DataFormatString.Length > 0)
195                                 res = string.Format (DataFormatString, value);
196                         else
197                                 res = value.ToString ();
198                                 
199                         if (encode) return HttpUtility.HtmlEncode (res);
200                         else return res;
201                 }
202                 
203                 protected virtual object GetValue (Control controlContainer)
204                 {
205                         if (DesignMode)
206                                 return GetDesignTimeValue ();
207                         else
208                                 return GetBoundValue (controlContainer);
209                 }
210                 
211                 protected virtual object GetDesignTimeValue ()
212                 {
213                         return "Databound";
214                 }
215
216                 object GetBoundValue (Control controlContainer)
217                 {
218                         object dataItem = DataBinder.GetDataItem (controlContainer);
219                         if (dataItem == null)
220                                 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
221
222                         if (DataField == ThisExpression)
223                                 return dataItem.ToString ();
224                         else if (DataField == string.Empty)
225                                 return null;
226
227                         return DataBinder.GetPropertyValue (dataItem, DataField);
228                 }
229                 
230                 protected virtual void OnDataBindField (object sender, EventArgs e)
231                 {
232                         Control cell = (Control) sender;
233                         Control controlContainer = cell.BindingContainer;
234                         if (!(controlContainer is INamingContainer))
235                                 throw new HttpException ("A DataControlField must be within an INamingContainer.");
236                         object val = GetValue (controlContainer);
237
238                         if (cell.Controls.Count > 0) {
239                                 TextBox box = (TextBox) cell.Controls [0];
240                                 if (ApplyFormatInEditMode)
241                                         box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
242                                 else
243                                         box.Text = val != null ? val.ToString() : NullDisplayText;
244                         }
245                         else
246                                 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
247                 }
248                 
249                 protected override DataControlField CreateField ()
250                 {
251                         return new BoundField ();
252                 }
253                 
254                 protected override void CopyProperties (DataControlField newField)
255                 {
256                         base.CopyProperties (newField);
257                         BoundField field = (BoundField) newField;
258                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
259                         field.DataField = DataField;
260                         field.DataFormatString = DataFormatString;
261                         field.NullDisplayText = NullDisplayText;
262                         field.ReadOnly = ReadOnly;
263                         field.HtmlEncode = HtmlEncode;
264                 }
265
266                 // MSDN: The ValidateSupportsCallback method is a helper method used to determine 
267                 // whether the controls contained in a BoundField object support callbacks. 
268                 // This method has been implemented as an empty method (a method that does not contain 
269                 // any code) to indicate that callbacks are supported.
270                 public override void ValidateSupportsCallback ()
271                 {
272                 }
273
274         }
275 }
276 #endif