ButtonField.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // System.Web.UI.WebControls.ButtonField.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Reflection;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Web.UI;
  34. using System.ComponentModel;
  35. using System.Security.Permissions;
  36. namespace System.Web.UI.WebControls {
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class ButtonField : ButtonFieldBase
  40. {
  41. PropertyDescriptor boundProperty;
  42. [WebCategoryAttribute ("Behavior")]
  43. [DefaultValueAttribute ("")]
  44. public virtual string CommandName {
  45. get {
  46. object ob = ViewState ["CommandName"];
  47. if (ob != null) return (string) ob;
  48. return "";
  49. }
  50. set {
  51. ViewState ["CommandName"] = value;
  52. OnFieldChanged ();
  53. }
  54. }
  55. [WebCategoryAttribute ("Data")]
  56. [DefaultValueAttribute ("")]
  57. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  58. public virtual string DataTextField {
  59. get {
  60. object ob = ViewState ["DataTextField"];
  61. if (ob != null) return (string) ob;
  62. return "";
  63. }
  64. set {
  65. ViewState ["DataTextField"] = value;
  66. OnFieldChanged ();
  67. }
  68. }
  69. [WebCategoryAttribute ("Data")]
  70. [DefaultValueAttribute ("")]
  71. public virtual string DataTextFormatString {
  72. get {
  73. object ob = ViewState ["DataTextFormatString"];
  74. if (ob != null) return (string) ob;
  75. return "";
  76. }
  77. set {
  78. ViewState ["DataTextFormatString"] = value;
  79. OnFieldChanged ();
  80. }
  81. }
  82. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  83. [WebCategoryAttribute ("Appearance")]
  84. [DefaultValueAttribute ("")]
  85. [UrlPropertyAttribute]
  86. public virtual string ImageUrl {
  87. get {
  88. object ob = ViewState ["ImageUrl"];
  89. if (ob != null) return (string) ob;
  90. return "";
  91. }
  92. set {
  93. ViewState ["ImageUrl"] = value;
  94. OnFieldChanged ();
  95. }
  96. }
  97. [LocalizableAttribute (true)]
  98. [WebCategoryAttribute ("Appearance")]
  99. [DefaultValueAttribute ("")]
  100. public virtual string Text {
  101. get {
  102. object ob = ViewState ["Text"];
  103. if (ob != null) return (string) ob;
  104. return "";
  105. }
  106. set {
  107. ViewState ["Text"] = value;
  108. OnFieldChanged ();
  109. }
  110. }
  111. public override bool Initialize (bool sortingEnabled, Control control)
  112. {
  113. return base.Initialize (sortingEnabled, control);
  114. }
  115. protected virtual string FormatDataTextValue (object value)
  116. {
  117. if (DataTextFormatString.Length > 0)
  118. return string.Format (DataTextFormatString, value);
  119. else if (value == null)
  120. return string.Empty;
  121. else
  122. return value.ToString ();
  123. }
  124. public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  125. {
  126. string index = rowIndex.ToString ();
  127. if (cellType == DataControlCellType.DataCell) {
  128. DataControlButton btn = new DataControlButton (Control);
  129. btn.CommandName = CommandName;
  130. btn.CommandArgument = index;
  131. if (DataTextField != "") {
  132. if ((rowState & DataControlRowState.Insert) == 0)
  133. cell.DataBinding += new EventHandler (OnDataBindField);
  134. }
  135. else {
  136. btn.Text = Text;
  137. btn.ButtonType = ButtonType;
  138. if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
  139. }
  140. cell.Controls.Add (btn);
  141. }
  142. else
  143. base.InitializeCell (cell, cellType, rowState, rowIndex);
  144. }
  145. void OnDataBindField (object sender, EventArgs e)
  146. {
  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;
  152. }
  153. object GetBoundValue (Control controlContainer)
  154. {
  155. IDataItemContainer dic = controlContainer as IDataItemContainer;
  156. if (boundProperty == null) {
  157. boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
  158. if (boundProperty == null)
  159. new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
  160. }
  161. return boundProperty.GetValue (dic.DataItem);
  162. }
  163. protected override DataControlField CreateField ()
  164. {
  165. return new ButtonField ();
  166. }
  167. protected override void CopyProperties (DataControlField newField)
  168. {
  169. base.CopyProperties (newField);
  170. ButtonField field = (ButtonField) newField;
  171. field.CommandName = CommandName;
  172. field.DataTextField = DataTextField;
  173. field.DataTextFormatString = DataTextFormatString;
  174. field.ImageUrl = ImageUrl;
  175. field.Text = Text;
  176. }
  177. }
  178. }
  179. #endif