2007-01-01 Igor Zelmanovich <igorz@mainsoft.com>
[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 = IsEditable (rowState);
142                         if (editable) {
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 = IsEditable (rowState);
172                         if (editable) {
173                                 TextBox box = new TextBox ();
174                                 box.ID = cell.ClientID;
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                         if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
193                                 if (NullDisplayText.Length == 0) {
194                                         encode = false;
195                                         res = "&nbsp;";
196                                 }
197                                 else
198                                         res = NullDisplayText;
199                         }
200                         else if (DataFormatString.Length > 0)
201                                 res = string.Format (DataFormatString, value);
202                         else
203                                 res = value.ToString ();
204                                 
205                         if (encode) return HttpUtility.HtmlEncode (res);
206                         else return res;
207                 }
208                 
209                 protected virtual object GetValue (Control controlContainer)
210                 {
211                         if (DesignMode)
212                                 return GetDesignTimeValue ();
213                         else
214                                 return GetBoundValue (controlContainer);
215                 }
216                 
217                 protected virtual object GetDesignTimeValue ()
218                 {
219                         return "Databound";
220                 }
221
222                 object GetBoundValue (Control controlContainer)
223                 {
224                         object dataItem = DataBinder.GetDataItem (controlContainer);
225                         if (dataItem == null)
226                                 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
227
228                         if (DataField == ThisExpression)
229                                 return dataItem.ToString ();
230                         else if (DataField == string.Empty)
231                                 return null;
232
233                         return DataBinder.GetPropertyValue (dataItem, DataField);
234                 }
235                 
236                 protected virtual void OnDataBindField (object sender, EventArgs e)
237                 {
238                         Control cell = (Control) sender;
239                         Control controlContainer = cell.BindingContainer;
240                         if (!(controlContainer is INamingContainer))
241                                 throw new HttpException ("A DataControlField must be within an INamingContainer.");
242                         object val = GetValue (controlContainer);
243
244                         if (cell.Controls.Count > 0) {
245                                 TextBox box = (TextBox) cell.Controls [0];
246                                 if (ApplyFormatInEditMode)
247                                         box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
248                                 else
249                                         box.Text = val != null ? val.ToString() : NullDisplayText;
250                         }
251                         else
252                                 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
253                 }
254                 
255                 protected override DataControlField CreateField ()
256                 {
257                         return new BoundField ();
258                 }
259                 
260                 protected override void CopyProperties (DataControlField newField)
261                 {
262                         base.CopyProperties (newField);
263                         BoundField field = (BoundField) newField;
264                         field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
265                         field.DataField = DataField;
266                         field.DataFormatString = DataFormatString;
267                         field.NullDisplayText = NullDisplayText;
268                         field.ReadOnly = ReadOnly;
269                         field.HtmlEncode = HtmlEncode;
270                 }
271
272                 // MSDN: The ValidateSupportsCallback method is a helper method used to determine 
273                 // whether the controls contained in a BoundField object support callbacks. 
274                 // This method has been implemented as an empty method (a method that does not contain 
275                 // any code) to indicate that callbacks are supported.
276                 public override void ValidateSupportsCallback ()
277                 {
278                 }
279
280         }
281 }
282 #endif