2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 using System;
13 using System.ComponentModel;
14 using System.Web;
15 using System.Web.UI;
16
17 namespace System.Web.UI.WebControls
18 {
19         public class BoundColumn : DataGridColumn
20         {
21                 public static readonly string thisExpr = "!";
22
23                 private bool boundFieldDescriptionValid;
24                 private string boundField;
25                 private string formatting;
26                 
27                 private PropertyDescriptor desc;
28
29                 public BoundColumn(): base()
30                 {
31                 }
32
33                 public override void Initialize()
34                 {
35                         base.Initialize();
36
37                         desc       = null;
38                         boundField = DataField;
39                         formatting = DataFormatString;
40                         boundFieldDescriptionValid = false;
41                 }
42
43                 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
44                 {
45                         base.InitializeCell(cell, columnIndex, itemType);
46
47                         Control bindCtrl = null;
48                         Control toAdd    = null;
49                         switch(itemType)
50                         {
51                                 case ListItemType.Item : goto case ListItemType.SelectedItem;
52                                 case ListItemType.AlternatingItem
53                                                        : goto case ListItemType.SelectedItem;
54                                 case ListItemType.SelectedItem
55                                                        : if(DataField.Length != 0)
56                                                                 bindCtrl = cell;
57                                                          break;
58                                 case ListItemType.EditItem
59                                                            : if(!ReadOnly)
60                                                              {
61                                                                 TextBox box = new TextBox();
62                                                                 toAdd = box;
63                                                                 if(DataField.Length != 0)
64                                                                         bindCtrl = box;
65                                                              } else 
66                                                                      goto case ListItemType.SelectedItem;
67
68                                                              break;
69                         }
70                         if(toAdd != null)
71                                 cell.Controls.Add(toAdd);
72                         if(bindCtrl != null)
73                                 bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
74                         //throw new NotImplementedException();
75                 }
76
77                 private void OnDataBindColumn(object sender, EventArgs e)
78                 {
79                         Control senderCtrl = (Control)sender;
80                         DataGridItem item  = (DataGridItem)senderCtrl.NamingContainer;
81                         object       data  = item.DataItem;
82
83                         if(!boundFieldDescriptionValid)
84                         {
85                                 if(boundField != BoundColumn.thisExpr)
86                                 {
87                                         desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
88                                         if(desc == null && !DesignMode)
89                                         {
90                                                 throw new HttpException(
91                                                           HttpRuntime.FormatResourceString("File_Not_Found",
92                                                                                            boundField));
93                                         }
94                                         boundFieldDescriptionValid = true;
95                                 }
96                         }
97                         object value = data;
98                         string val = String.Empty;
99                         if(desc == null && DesignMode)
100                         {
101                                 throw new NotImplementedException();
102                         } else
103                         {
104                                 if(desc != null)
105                                         value = desc.GetValue(data);
106                                 val = FormatDataValue(value);
107                         }
108                         if(senderCtrl is TableCell)
109                         {
110                                 if(val.Length == 0)
111                                         val = "&nbsp;";
112                                 ((TableCell)senderCtrl).Text = val;
113                         } else
114                         {
115                                 ((TextBox)senderCtrl).Text = val;
116                         }
117                 }
118
119                 [DefaultValue (""), WebCategory ("Misc")]
120                 [WebSysDescription ("The field that this column is bound to.")]
121                 public virtual string DataField
122                 {
123                         get
124                         {
125                                 object o = ViewState["DataField"];
126                                 if(o != null)
127                                         return (string)o;
128                                 return String.Empty;
129                         }
130                         set
131                         {
132                                 ViewState["DataField"] = value;
133                                 OnColumnChanged();
134                         }
135                 }
136
137                 [DefaultValue (""), WebCategory ("Misc")]
138                 [WebSysDescription ("A format string that is applied to the data value.")]
139                 public virtual string DataFormatString
140                 {
141                         get
142                         {
143                                 object o = ViewState["DataFormatString"];
144                                 if(o != null)
145                                         return (string)o;
146                                 return String.Empty;
147                         }
148                         set
149                         {
150                                 ViewState["DataFormatString"] = value;
151                                 OnColumnChanged();
152                         }
153                 }
154
155                 [DefaultValue (false), WebCategory ("Misc")]
156                 [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
157                 public virtual bool ReadOnly
158                 {
159                         get
160                         {
161                                 object o = ViewState["ReadOnly"];
162                                 if(o != null)
163                                         return (bool)o;
164                                 return false;
165                         }
166                         set
167                         {
168                                 ViewState["ReadOnly"] = value;
169                         }
170                 }
171
172                 protected virtual string FormatDataValue(Object dataValue)
173                 {
174                         string retVal = String.Empty;
175                         if(dataValue != null)
176                         {
177                                 if(formatting.Length == 0)
178                                         retVal = dataValue.ToString();
179                                 else
180                                         retVal = String.Format(formatting, dataValue);
181                         }
182                         return retVal;
183                 }
184         }
185 }