ChangeLog: Updated ChangeLog.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundColumn.cs
1 //
2 // System.Web.UI.WebControls.BoundColumn.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //\r
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.Web;
36 using System.Web.UI;
37
38 namespace System.Web.UI.WebControls
39 {
40         public class BoundColumn : DataGridColumn
41         {
42                 public static readonly string thisExpr = "!";
43
44                 private bool boundFieldDescriptionValid;
45                 private string boundField;
46                 private string formatting;
47                 
48                 private PropertyDescriptor desc;
49
50                 public BoundColumn(): base()
51                 {
52                 }
53
54                 public override void Initialize()
55                 {
56                         base.Initialize();
57
58                         desc       = null;
59                         boundField = DataField;
60                         formatting = DataFormatString;
61                         boundFieldDescriptionValid = false;
62                 }
63
64                 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
65                 {
66                         base.InitializeCell(cell, columnIndex, itemType);
67
68                         Control bindCtrl = null;
69                         Control toAdd    = null;
70                         switch(itemType)
71                         {
72                                 case ListItemType.Item : goto case ListItemType.SelectedItem;
73                                 case ListItemType.AlternatingItem
74                                                        : goto case ListItemType.SelectedItem;
75                                 case ListItemType.SelectedItem
76                                                        : if(DataField.Length != 0)
77                                                                 bindCtrl = cell;
78                                                          break;
79                                 case ListItemType.EditItem
80                                                            : if(!ReadOnly)
81                                                              {
82                                                                 TextBox box = new TextBox();
83                                                                 toAdd = box;
84                                                                 if(DataField.Length != 0)
85                                                                         bindCtrl = box;
86                                                              } else 
87                                                                      goto case ListItemType.SelectedItem;
88
89                                                              break;
90                         }
91                         if(toAdd != null)
92                                 cell.Controls.Add(toAdd);
93                         if(bindCtrl != null)
94                                 bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
95                         //throw new NotImplementedException();
96                 }
97
98                 private void OnDataBindColumn(object sender, EventArgs e)
99                 {
100                         Control senderCtrl = (Control)sender;
101                         DataGridItem item  = (DataGridItem)senderCtrl.NamingContainer;
102                         object       data  = item.DataItem;
103
104                         if(!boundFieldDescriptionValid)
105                         {
106                                 if(boundField != BoundColumn.thisExpr)
107                                 {
108                                         desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
109                                         if(desc == null && !DesignMode)
110                                         {
111                                                 throw new HttpException(
112                                                           HttpRuntime.FormatResourceString("File_Not_Found",
113                                                                                            boundField));
114                                         }
115                                         boundFieldDescriptionValid = true;
116                                 }
117                         }
118                         object value = data;
119                         string val = String.Empty;
120                         if(desc == null && DesignMode)
121                         {
122                                 throw new NotImplementedException();
123                         } else
124                         {
125                                 if(desc != null)
126                                         value = desc.GetValue(data);
127                                 val = FormatDataValue(value);
128                         }
129                         if(senderCtrl is TableCell)
130                         {
131                                 if(val.Length == 0)
132                                         val = " ";
133                                 ((TableCell)senderCtrl).Text = val;
134                         } else
135                         {
136                                 ((TextBox)senderCtrl).Text = val;
137                         }
138                 }
139
140                 [DefaultValue (""), WebCategory ("Misc")]
141                 [WebSysDescription ("The field that this column is bound to.")]
142                 public virtual string DataField
143                 {
144                         get
145                         {
146                                 object o = ViewState["DataField"];
147                                 if(o != null)
148                                         return (string)o;
149                                 return String.Empty;
150                         }
151                         set
152                         {
153                                 ViewState["DataField"] = value;
154                                 OnColumnChanged();
155                         }
156                 }
157
158                 [DefaultValue (""), WebCategory ("Misc")]
159                 [WebSysDescription ("A format string that is applied to the data value.")]
160                 public virtual string DataFormatString
161                 {
162                         get
163                         {
164                                 object o = ViewState["DataFormatString"];
165                                 if(o != null)
166                                         return (string)o;
167                                 return String.Empty;
168                         }
169                         set
170                         {
171                                 ViewState["DataFormatString"] = value;
172                                 OnColumnChanged();
173                         }
174                 }
175
176                 [DefaultValue (false), WebCategory ("Misc")]
177                 [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
178                 public virtual bool ReadOnly
179                 {
180                         get
181                         {
182                                 object o = ViewState["ReadOnly"];
183                                 if(o != null)
184                                         return (bool)o;
185                                 return false;
186                         }
187                         set
188                         {
189                                 ViewState["ReadOnly"] = value;
190                         }
191                 }
192
193                 protected virtual string FormatDataValue(Object dataValue)
194                 {
195                         string retVal = String.Empty;
196                         if(dataValue != null)
197                         {
198                                 if(formatting.Length == 0)
199                                         retVal = dataValue.ToString();
200                                 else
201                                         retVal = String.Format(formatting, dataValue);
202                         }
203                         return retVal;
204                 }
205         }
206 }