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 ("")]
\r
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 ("")]
\r
85 [UrlPropertyAttribute]
\r
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) {
126 DataControlButton btn = new DataControlButton (Control);
127 btn.CommandName = CommandName;
128 btn.CommandArgument = index;
130 if (DataTextField != "") {
131 if ((rowState & DataControlRowState.Insert) == 0)
132 cell.DataBinding += new EventHandler (OnDataBindField);
136 btn.ButtonType = ButtonType;
137 if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
139 cell.Controls.Add (btn);
142 base.InitializeCell (cell, cellType, rowState, rowIndex);
145 void OnDataBindField (object sender, EventArgs e)
147 DataControlFieldCell cell = (DataControlFieldCell) sender;
148 DataControlButton btn = (DataControlButton) cell.Controls [0];
149 btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
150 if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
151 btn.ButtonType = ButtonType;
154 object GetBoundValue (Control controlContainer)
156 IDataItemContainer dic = controlContainer as IDataItemContainer;
157 if (boundProperty == null) {
158 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
159 if (boundProperty == null)
160 new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
162 return boundProperty.GetValue (dic.DataItem);
165 protected override DataControlField CreateField ()
167 return new ButtonField ();
170 protected override void CopyProperties (DataControlField newField)
172 base.CopyProperties (newField);
173 ButtonField field = (ButtonField) newField;
174 field.CommandName = CommandName;
175 field.DataTextField = DataTextField;
176 field.DataTextFormatString = DataTextFormatString;
177 field.ImageUrl = ImageUrl;
182 public override void ValidateSupportsCallback ()
184 throw new NotImplementedException ();