ButtonColumn.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // System.Web.UI.WebControls.ButtonColumn.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.UI.WebControls
  36. {
  37. public class ButtonColumn : DataGridColumn
  38. {
  39. private PropertyDescriptor textFieldDescriptor;
  40. public ButtonColumn(): base()
  41. {
  42. }
  43. public override void Initialize()
  44. {
  45. base.Initialize();
  46. textFieldDescriptor = null;
  47. }
  48. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  49. {
  50. base.InitializeCell(cell, columnIndex, itemType);
  51. if (Enum.IsDefined(typeof(ListItemType), itemType) &&
  52. itemType != ListItemType.Footer &&
  53. itemType != ListItemType.Header)
  54. {
  55. WebControl toDisplay = null;
  56. if(ButtonType == ButtonColumnType.PushButton)
  57. {
  58. Button b = new Button();
  59. b.Text = Text;
  60. b.CommandName = CommandName;
  61. b.CausesValidation = false;
  62. toDisplay = b;
  63. } else
  64. {
  65. LinkButton lb = new DataGridLinkButton();
  66. lb.Text = Text;
  67. lb.CommandName = CommandName;
  68. lb.CausesValidation = false;
  69. toDisplay = lb;
  70. }
  71. if(DataTextField.Length > 0)
  72. {
  73. toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);
  74. }
  75. cell.Controls.Add(toDisplay);
  76. }
  77. }
  78. private void OnDataBindButtonColumn(object sender, EventArgs e)
  79. {
  80. Control ctrl = (Control)sender;
  81. object item = ((DataGridItem)ctrl.NamingContainer).DataItem;
  82. if(textFieldDescriptor == null)
  83. {
  84. textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);
  85. if(textFieldDescriptor == null && !DesignMode)
  86. throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));
  87. }
  88. string text;
  89. if(textFieldDescriptor != null)
  90. {
  91. text = FormatDataTextValue(textFieldDescriptor.GetValue(item));
  92. } else
  93. {
  94. text = "Sample_DataBound_Text";
  95. }
  96. if(ctrl is LinkButton)
  97. {
  98. ((LinkButton)ctrl).Text = text;
  99. }
  100. else
  101. {
  102. ((Button)ctrl).Text = text;
  103. }
  104. }
  105. protected virtual string FormatDataTextValue(object dataTextValue)
  106. {
  107. string retVal = null;
  108. if(dataTextValue != null)
  109. {
  110. if(DataTextFormatString.Length > 0)
  111. {
  112. retVal = String.Format(DataTextFormatString, dataTextValue);
  113. }
  114. else
  115. {
  116. retVal = dataTextValue.ToString();
  117. }
  118. }
  119. return retVal;
  120. }
  121. // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
  122. // because all metadata in this namespace has WebSysDescriptions
  123. [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
  124. [Description ("The type of button used in this column.")]
  125. public virtual ButtonColumnType ButtonType
  126. {
  127. get
  128. {
  129. object o = ViewState["ButtonType"];
  130. if(o!=null)
  131. return (ButtonColumnType)o;
  132. return ButtonColumnType.LinkButton;
  133. }
  134. set
  135. {
  136. if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))
  137. throw new ArgumentException();
  138. ViewState["ButtonType"] = value;
  139. }
  140. }
  141. [DefaultValue (""), WebCategory ("Misc")]
  142. [Description ("The command assigned to this column.")]
  143. public virtual string CommandName
  144. {
  145. get
  146. {
  147. string cn = (string)ViewState["CommandName"];
  148. if(cn!=null)
  149. return cn;
  150. return String.Empty;
  151. }
  152. set
  153. {
  154. ViewState["CommandName"] = value;
  155. }
  156. }
  157. [DefaultValue (""), WebCategory ("Misc")]
  158. [Description ("The datafield that is bound to the text property.")]
  159. public virtual string DataTextField
  160. {
  161. get
  162. {
  163. string dtf = (string)ViewState["DataTextField"];
  164. if(dtf!=null)
  165. return dtf;
  166. return String.Empty;
  167. }
  168. set
  169. {
  170. ViewState["DataTextField"] = value;
  171. }
  172. }
  173. [DefaultValue (""), WebCategory ("Misc")]
  174. [Description ("A format that is applied to the bound text property.")]
  175. public virtual string DataTextFormatString
  176. {
  177. get
  178. {
  179. string dtfs = (string)ViewState["DataTextFormatString"];
  180. if(dtfs!=null)
  181. return dtfs;
  182. return String.Empty;
  183. }
  184. set
  185. {
  186. ViewState["DataTextFormatString"] = value;
  187. }
  188. }
  189. [DefaultValue (""), WebCategory ("Misc")]
  190. [Description ("The text used for this button.")]
  191. public virtual string Text
  192. {
  193. get
  194. {
  195. string text = (string)ViewState["Text"];
  196. if(text!=null)
  197. return text;
  198. return String.Empty;
  199. }
  200. set
  201. {
  202. ViewState["Text"] = value;
  203. }
  204. }
  205. }
  206. }