ButtonColumn.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Web.UI.WebControls.ButtonColumn.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. namespace System.Web.UI.WebControls {
  30. public class ButtonColumn : DataGridColumn {
  31. [DefaultValue(ButtonColumnType.LinkButton)]
  32. [Description("The type of button contained within the column.")]
  33. public virtual ButtonColumnType ButtonType
  34. {
  35. get {
  36. return (ButtonColumnType) ViewState.GetInt ("LinkButton",
  37. (int) ButtonColumnType.LinkButton);
  38. }
  39. set {
  40. ViewState ["LinkButton"] = value;
  41. }
  42. }
  43. [DefaultValue("")]
  44. [Description("The command associated with the button.")]
  45. public virtual string CommandName
  46. {
  47. get {
  48. return ViewState.GetString ("CommandName", String.Empty);
  49. }
  50. set {
  51. ViewState ["CommandName"] = value;
  52. }
  53. }
  54. [DefaultValue("")]
  55. [Description("The field bound to the text property of the button.")]
  56. public virtual string DataTextField
  57. {
  58. get {
  59. return ViewState.GetString ("DataTextField", String.Empty);
  60. }
  61. set {
  62. ViewState ["DataTextField"] = value;
  63. }
  64. }
  65. [DefaultValue("")]
  66. [Description("The formatting applied to the value bound to the Text property.")]
  67. public virtual string DataTextFormatString
  68. {
  69. get {
  70. return ViewState.GetString ("DataTextFormatString",
  71. String.Empty);
  72. }
  73. set {
  74. ViewState ["DataTextFormatString"] = value;
  75. }
  76. }
  77. [DefaultValue("")]
  78. [Description("The text used for the button.")]
  79. public virtual string Text
  80. {
  81. get {
  82. return ViewState.GetString ("Text", String.Empty);
  83. }
  84. set {
  85. ViewState ["Text"] = value;
  86. }
  87. }
  88. public override void Initialize ()
  89. {
  90. /* No documentation for this method, so it's
  91. * only here to keep corcompare quiet
  92. */
  93. base.Initialize ();
  94. }
  95. public override void InitializeCell (TableCell cell,
  96. int columnIndex,
  97. ListItemType itemType)
  98. {
  99. base.InitializeCell (cell, columnIndex, itemType);
  100. if (itemType != ListItemType.Header &&
  101. itemType != ListItemType.Footer) {
  102. switch (ButtonType) {
  103. case ButtonColumnType.LinkButton:
  104. {
  105. LinkButton butt = new LinkButton ();
  106. butt.Text = Text;
  107. butt.CommandName = CommandName;
  108. cell.Controls.Add (butt);
  109. }
  110. break;
  111. case ButtonColumnType.PushButton:
  112. {
  113. Button butt = new Button ();
  114. butt.Text = Text;
  115. butt.CommandName = CommandName;
  116. cell.Controls.Add (butt);
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. protected virtual string FormatDataTextValue (object dataTextValue)
  123. {
  124. if (DataTextFormatString == String.Empty)
  125. return dataTextValue.ToString ();
  126. return String.Format (DataTextFormatString, dataTextValue);
  127. }
  128. }
  129. }