2 // System.Web.UI.WebControls.ButtonColumn.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 using System.ComponentModel;
\r
15 using System.Web.UI;
\r
17 namespace System.Web.UI.WebControls
\r
19 public class ButtonColumn : DataGridColumn
\r
21 private PropertyDescriptor textFieldDescriptor;
\r
23 public ButtonColumn(): base()
\r
27 public override void Initialize()
\r
30 textFieldDescriptor = null;
\r
33 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
\r
35 base.InitializeCell(cell, columnIndex, itemType);
\r
36 if (Enum.IsDefined(typeof(ListItemType), itemType) &&
\r
37 itemType != ListItemType.Footer &&
\r
38 itemType != ListItemType.Header)
\r
40 WebControl toDisplay = null;
\r
41 if(ButtonType == ButtonColumnType.PushButton)
\r
43 Button b = new Button();
\r
45 b.CommandName = CommandName;
\r
46 b.CausesValidation = false;
\r
50 LinkButton lb = new DataGridLinkButton();
\r
52 lb.CommandName = CommandName;
\r
53 lb.CausesValidation = false;
\r
56 if(DataTextField.Length > 0)
\r
58 toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);
\r
60 cell.Controls.Add(toDisplay);
\r
64 private void OnDataBindButtonColumn(object sender, EventArgs e)
\r
66 Control ctrl = (Control)sender;
\r
67 object item = ((DataGridItem)ctrl.NamingContainer).DataItem;
\r
68 if(textFieldDescriptor == null)
\r
70 textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);
\r
71 if(textFieldDescriptor == null && !DesignMode)
\r
72 throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));
\r
75 if(textFieldDescriptor != null)
\r
77 text = FormatDataTextValue(textFieldDescriptor.GetValue(item));
\r
80 text = "Sample_DataBound_Text";
\r
82 if(ctrl is LinkButton)
\r
84 ((LinkButton)ctrl).Text = text;
\r
88 ((Button)ctrl).Text = text;
\r
92 protected virtual string FormatDataTextValue(object dataTextValue)
\r
94 string retVal = null;
\r
95 if(dataTextValue != null)
\r
97 if(DataTextFormatString.Length > 0)
\r
99 retVal = String.Format(DataTextFormatString, dataTextValue);
\r
103 retVal = dataTextValue.ToString();
\r
109 // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
110 // because all metadata in this namespace has WebSysDescriptions
\r
112 [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
113 [Description ("The type of button used in this column.")]
\r
114 public virtual ButtonColumnType ButtonType
\r
118 object o = ViewState["ButtonType"];
\r
120 return (ButtonColumnType)o;
\r
121 return ButtonColumnType.LinkButton;
\r
125 if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))
\r
126 throw new ArgumentException();
\r
127 ViewState["ButtonType"] = value;
\r
131 [DefaultValue (""), WebCategory ("Misc")]
132 [Description ("The command assigned to this column.")]
\r
133 public virtual string CommandName
\r
137 string cn = (string)ViewState["CommandName"];
\r
140 return String.Empty;
\r
144 ViewState["CommandName"] = value;
\r
148 [DefaultValue (""), WebCategory ("Misc")]
149 [Description ("The datafield that is bound to the text property.")]
\r
150 public virtual string DataTextField
\r
154 string dtf = (string)ViewState["DataTextField"];
\r
157 return String.Empty;
\r
161 ViewState["DataTextField"] = value;
\r
165 [DefaultValue (""), WebCategory ("Misc")]
166 [Description ("A format that is applied to the bound text property.")]
\r
167 public virtual string DataTextFormatString
\r
171 string dtfs = (string)ViewState["DataTextFormatString"];
\r
174 return String.Empty;
\r
178 ViewState["DataTextFormatString"] = value;
\r
182 [DefaultValue (""), WebCategory ("Misc")]
183 [Description ("The text used for this button.")]
\r
184 public virtual string Text
\r
188 string text = (string)ViewState["Text"];
\r
191 return String.Empty;
\r
195 ViewState["Text"] = value;
\r