2 // System.Web.UI.WebControls.BoundColumn.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls {
35 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 public class BoundColumn : DataGridColumn {
39 private string data_format_string;
45 public static readonly string thisExpr = "!";
48 [WebSysDescription ("")]
49 [WebCategory ("Misc")]
50 public virtual string DataField
53 return ViewState.GetString ("DataField", String.Empty);
56 ViewState ["DataField"] = value;
61 [WebSysDescription ("")]
62 [WebCategory ("Misc")]
63 public virtual string DataFormatString
66 return ViewState.GetString ("DataFormatString", String.Empty);
69 ViewState ["DataFormatString"] = value;
74 [WebSysDescription ("")]
75 [WebCategory ("Misc")]
76 public virtual bool ReadOnly
79 return ViewState.GetBool ("ReadOnly", false);
82 ViewState ["ReadOnly"] = value;
86 public override void Initialize ()
88 data_format_string = DataFormatString;
91 public override void InitializeCell (TableCell cell, int columnIndex,
92 ListItemType itemType)
94 base.InitializeCell (cell, columnIndex, itemType);
96 string df = DataField;
99 case ListItemType.Item:
100 case ListItemType.SelectedItem:
101 case ListItemType.AlternatingItem:
102 if (df != null && df.Length != 0)
103 cell.DataBinding += new EventHandler (ItemDataBinding);
105 case ListItemType.EditItem:
106 if (ReadOnly && df != null && df.Length != 0) {
107 cell.DataBinding += new EventHandler (ItemDataBinding);
110 TextBox tb = new TextBox ();
111 if (df != null && df.Length != 0)
112 tb.DataBinding += new EventHandler (ItemDataBinding);
113 cell.Controls.Add (tb);
118 protected virtual string FormatDataValue (object dataValue)
120 if (dataValue == null)
123 if (data_format_string == String.Empty)
124 return dataValue.ToString ();
126 return String.Format (data_format_string, dataValue);
129 string GetValueFromItem (DataGridItem item)
132 if (DataField != thisExpr) {
133 val = DataBinder.Eval (item.DataItem, DataField);
138 string text = FormatDataValue (val);
139 return (text != "" ? text : " ");
142 void ItemDataBinding (object sender, EventArgs e)
144 Control ctrl = (Control) sender;
145 string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
147 TableCell cell = sender as TableCell;
149 TextBox tb = (TextBox) sender;