2 // System.Web.UI.WebControls.ButtonField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Collections;
33 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Security.Permissions;
38 namespace System.Web.UI.WebControls
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public class ButtonField : ButtonFieldBase
44 PropertyDescriptor boundProperty;
46 [DefaultValueAttribute ("")]
47 [WebSysDescription ("Raised when a Button Command is executed.")]
48 [WebCategoryAttribute ("Behavior")]
49 public virtual string CommandName {
50 get { return ViewState.GetString ("CommandName", String.Empty); }
52 ViewState ["CommandName"] = value;
57 [DefaultValueAttribute ("")]
58 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
59 [WebSysDescription ("")]
60 [WebCategoryAttribute ("Data")]
61 public virtual string DataTextField {
62 get { return ViewState.GetString ("DataTextField", String.Empty); }
64 ViewState ["DataTextField"] = value;
69 [DefaultValueAttribute ("")]
70 [WebSysDescription ("")]
71 [WebCategoryAttribute ("Data")]
72 public virtual string DataTextFormatString {
73 get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
75 ViewState ["DataTextFormatString"] = value;
80 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
81 [WebSysDescription ("")]
82 [WebCategoryAttribute ("Appearance")]
83 [DefaultValueAttribute ("")]
84 [UrlPropertyAttribute]
85 public virtual string ImageUrl {
86 get { return ViewState.GetString ("ImageUrl", String.Empty); }
88 ViewState ["ImageUrl"] = value;
93 [LocalizableAttribute (true)]
94 [DefaultValueAttribute ("")]
95 [WebSysDescription ("")]
96 [WebCategoryAttribute ("Appearance")]
97 public virtual string Text {
98 get { return ViewState.GetString ("Text", String.Empty); }
100 ViewState ["Text"] = value;
105 public override bool Initialize (bool sortingEnabled, Control control)
107 return base.Initialize (sortingEnabled, control);
110 protected virtual string FormatDataTextValue (object dataTextValue)
112 if (DataTextFormatString.Length > 0)
113 return String.Format (DataTextFormatString, dataTextValue);
114 else if (dataTextValue == null)
117 return dataTextValue.ToString ();
120 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
122 string index = rowIndex.ToString ();
124 if (cellType == DataControlCellType.DataCell) {
126 IDataControlButton btn = DataControlButton.CreateButton (ButtonType, Control, Text, ImageUrl, CommandName, index, false);
128 if (CausesValidation) {
129 btn.Container = null;
130 btn.CausesValidation = true;
131 btn.ValidationGroup = ValidationGroup;
134 if (!String.IsNullOrEmpty (DataTextField)) {
135 if ((rowState & DataControlRowState.Insert) == 0)
136 cell.DataBinding += new EventHandler (OnDataBindField);
138 cell.Controls.Add ((Control) btn);
140 base.InitializeCell (cell, cellType, rowState, rowIndex);
143 void OnDataBindField (object sender, EventArgs e)
145 DataControlFieldCell cell = (DataControlFieldCell) sender;
146 IDataControlButton btn = (IDataControlButton) cell.Controls [0];
147 btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
150 object GetBoundValue (Control controlContainer)
152 IDataItemContainer dic = controlContainer as IDataItemContainer;
153 if (boundProperty == null) {
154 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
155 if (boundProperty == null)
156 throw new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
158 return boundProperty.GetValue (dic.DataItem);
161 protected override DataControlField CreateField ()
163 return new ButtonField ();
166 protected override void CopyProperties (DataControlField newField)
168 base.CopyProperties (newField);
169 ButtonField field = (ButtonField) newField;
170 field.CommandName = CommandName;
171 field.DataTextField = DataTextField;
172 field.DataTextFormatString = DataTextFormatString;
173 field.ImageUrl = ImageUrl;
177 // MSDN: The ValidateSupportsCallback method is a helper method used to determine
178 // whether the controls contained in a BoundField object support callbacks.
179 // This method has been implemented as an empty method (a method that does not
180 // contain any code) to indicate that callbacks are supported.
181 public override void ValidateSupportsCallback ()