2 // System.Web.UI.WebControls.ButtonField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 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.
32 using System.Reflection;
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.ComponentModel;
37 using System.Security.Permissions;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class ButtonField : ButtonFieldBase
45 PropertyDescriptor boundProperty;
47 [DefaultValueAttribute ("")]
48 [WebSysDescription ("Raised when a Button Command is executed.")]
49 [WebCategoryAttribute ("Behavior")]
50 public virtual string CommandName {
51 get { return ViewState.GetString ("CommandName", ""); }
53 ViewState ["CommandName"] = value;
58 [DefaultValueAttribute ("")]
59 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
60 [WebSysDescription ("")]
61 [WebCategoryAttribute ("Data")]
62 public virtual string DataTextField {
63 get { return ViewState.GetString ("DataTextField", ""); }
65 ViewState ["DataTextField"] = value;
70 [DefaultValueAttribute ("")]
71 [WebSysDescription ("")]
72 [WebCategoryAttribute ("Data")]
73 public virtual string DataTextFormatString {
74 get { return ViewState.GetString ("DataTextFormatString", ""); }
76 ViewState ["DataTextFormatString"] = value;
81 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
82 [WebSysDescription ("")]
83 [WebCategoryAttribute ("Appearance")]
84 [DefaultValueAttribute ("")]
85 [UrlPropertyAttribute]
86 public virtual string ImageUrl {
87 get { return ViewState.GetString ("ImageUrl", ""); }
89 ViewState ["ImageUrl"] = value;
94 [LocalizableAttribute (true)]
95 [DefaultValueAttribute ("")]
96 [WebSysDescription ("")]
97 [WebCategoryAttribute ("Appearance")]
98 public virtual string Text {
99 get { return ViewState.GetString ("Text", ""); }
101 ViewState ["Text"] = value;
106 public override bool Initialize (bool sortingEnabled, Control control)
108 return base.Initialize (sortingEnabled, control);
111 protected virtual string FormatDataTextValue (object value)
113 if (DataTextFormatString.Length > 0)
114 return string.Format (DataTextFormatString, value);
115 else if (value == null)
118 return value.ToString ();
121 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
123 string index = rowIndex.ToString ();
125 if (cellType == DataControlCellType.DataCell) {
127 IDataControlButton btn = DataControlButton.CreateButton (ButtonType, Control, Text, ImageUrl, CommandName, index, false);
129 if (CausesValidation) {
130 btn.Container = null;
131 btn.CausesValidation = true;
132 btn.ValidationGroup = ValidationGroup;
135 if (DataTextField != "") {
136 if ((rowState & DataControlRowState.Insert) == 0)
137 cell.DataBinding += new EventHandler (OnDataBindField);
139 cell.Controls.Add ((Control) btn);
142 base.InitializeCell (cell, cellType, rowState, rowIndex);
145 void OnDataBindField (object sender, EventArgs e)
147 DataControlFieldCell cell = (DataControlFieldCell) sender;
148 IDataControlButton btn = (IDataControlButton) cell.Controls [0];
149 btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
152 object GetBoundValue (Control controlContainer)
154 IDataItemContainer dic = controlContainer as IDataItemContainer;
155 if (boundProperty == null) {
156 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
157 if (boundProperty == null)
158 new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
160 return boundProperty.GetValue (dic.DataItem);
163 protected override DataControlField CreateField ()
165 return new ButtonField ();
168 protected override void CopyProperties (DataControlField newField)
170 base.CopyProperties (newField);
171 ButtonField field = (ButtonField) newField;
172 field.CommandName = CommandName;
173 field.DataTextField = DataTextField;
174 field.DataTextFormatString = DataTextFormatString;
175 field.ImageUrl = ImageUrl;
179 // MSDN: The ValidateSupportsCallback method is a helper method used to determine
180 // whether the controls contained in a BoundField object support callbacks.
181 // This method has been implemented as an empty method (a method that does not
182 // contain any code) to indicate that callbacks are supported.
183 public override void ValidateSupportsCallback ()