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 // 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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.ComponentModel;
\r
36 using System.Web.UI;
\r
38 namespace System.Web.UI.WebControls
\r
40 public class ButtonColumn : DataGridColumn
\r
42 private PropertyDescriptor textFieldDescriptor;
\r
44 public ButtonColumn(): base()
\r
48 public override void Initialize()
\r
51 textFieldDescriptor = null;
\r
54 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
\r
56 base.InitializeCell(cell, columnIndex, itemType);
\r
57 if (Enum.IsDefined(typeof(ListItemType), itemType) &&
\r
58 itemType != ListItemType.Footer &&
\r
59 itemType != ListItemType.Header)
\r
61 WebControl toDisplay = null;
\r
62 if(ButtonType == ButtonColumnType.PushButton)
\r
64 Button b = new Button();
\r
66 b.CommandName = CommandName;
\r
67 b.CausesValidation = false;
\r
71 LinkButton lb = new DataGridLinkButton();
\r
73 lb.CommandName = CommandName;
\r
74 lb.CausesValidation = false;
\r
77 if(DataTextField.Length > 0)
\r
79 toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);
\r
81 cell.Controls.Add(toDisplay);
\r
85 private void OnDataBindButtonColumn(object sender, EventArgs e)
\r
87 Control ctrl = (Control)sender;
\r
88 object item = ((DataGridItem)ctrl.NamingContainer).DataItem;
\r
89 if(textFieldDescriptor == null)
\r
91 textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);
\r
92 if(textFieldDescriptor == null && !DesignMode)
\r
93 throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));
\r
96 if(textFieldDescriptor != null)
\r
98 text = FormatDataTextValue(textFieldDescriptor.GetValue(item));
\r
101 text = "Sample_DataBound_Text";
\r
103 if(ctrl is LinkButton)
\r
105 ((LinkButton)ctrl).Text = text;
\r
109 ((Button)ctrl).Text = text;
\r
113 protected virtual string FormatDataTextValue(object dataTextValue)
\r
115 string retVal = null;
\r
116 if(dataTextValue != null)
\r
118 if(DataTextFormatString.Length > 0)
\r
120 retVal = String.Format(DataTextFormatString, dataTextValue);
\r
124 retVal = dataTextValue.ToString();
\r
130 // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
131 // because all metadata in this namespace has WebSysDescriptions
\r
133 [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
134 [Description ("The type of button used in this column.")]
\r
135 public virtual ButtonColumnType ButtonType
\r
139 object o = ViewState["ButtonType"];
\r
141 return (ButtonColumnType)o;
\r
142 return ButtonColumnType.LinkButton;
\r
146 if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))
\r
147 throw new ArgumentException();
\r
148 ViewState["ButtonType"] = value;
\r
152 [DefaultValue (""), WebCategory ("Misc")]
153 [Description ("The command assigned to this column.")]
\r
154 public virtual string CommandName
\r
158 string cn = (string)ViewState["CommandName"];
\r
161 return String.Empty;
\r
165 ViewState["CommandName"] = value;
\r
169 [DefaultValue (""), WebCategory ("Misc")]
170 [Description ("The datafield that is bound to the text property.")]
\r
171 public virtual string DataTextField
\r
175 string dtf = (string)ViewState["DataTextField"];
\r
178 return String.Empty;
\r
182 ViewState["DataTextField"] = value;
\r
186 [DefaultValue (""), WebCategory ("Misc")]
187 [Description ("A format that is applied to the bound text property.")]
\r
188 public virtual string DataTextFormatString
\r
192 string dtfs = (string)ViewState["DataTextFormatString"];
\r
195 return String.Empty;
\r
199 ViewState["DataTextFormatString"] = value;
\r
203 [DefaultValue (""), WebCategory ("Misc")]
204 [Description ("The text used for this button.")]
\r
205 public virtual string Text
\r
209 string text = (string)ViewState["Text"];
\r
212 return String.Empty;
\r
216 ViewState["Text"] = value;
\r