ButtonColumn.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. [WebCategory ("Misc")]
  34. public virtual ButtonColumnType ButtonType
  35. {
  36. get {
  37. return (ButtonColumnType) ViewState.GetInt ("LinkButton",
  38. (int) ButtonColumnType.LinkButton);
  39. }
  40. set {
  41. ViewState ["LinkButton"] = value;
  42. }
  43. }
  44. [DefaultValue("")]
  45. [Description("The command associated with the button.")]
  46. [WebCategory ("Misc")]
  47. public virtual string CommandName
  48. {
  49. get {
  50. return ViewState.GetString ("CommandName", String.Empty);
  51. }
  52. set {
  53. ViewState ["CommandName"] = value;
  54. }
  55. }
  56. [DefaultValue("")]
  57. [Description("The field bound to the text property of the button.")]
  58. [WebCategory ("Misc")]
  59. public virtual string DataTextField
  60. {
  61. get {
  62. return ViewState.GetString ("DataTextField", String.Empty);
  63. }
  64. set {
  65. ViewState ["DataTextField"] = value;
  66. }
  67. }
  68. [DefaultValue("")]
  69. [Description("The formatting applied to the value bound to the Text property.")]
  70. [WebCategory ("Misc")]
  71. public virtual string DataTextFormatString
  72. {
  73. get {
  74. return ViewState.GetString ("DataTextFormatString",
  75. String.Empty);
  76. }
  77. set {
  78. ViewState ["DataTextFormatString"] = value;
  79. }
  80. }
  81. [DefaultValue("")]
  82. [Description("The text used for the button.")]
  83. [WebCategory ("Misc")]
  84. public virtual string Text
  85. {
  86. get {
  87. return ViewState.GetString ("Text", String.Empty);
  88. }
  89. set {
  90. ViewState ["Text"] = value;
  91. }
  92. }
  93. public override void Initialize ()
  94. {
  95. /* No documentation for this method, so it's
  96. * only here to keep corcompare quiet
  97. */
  98. base.Initialize ();
  99. }
  100. public override void InitializeCell (TableCell cell,
  101. int columnIndex,
  102. ListItemType itemType)
  103. {
  104. base.InitializeCell (cell, columnIndex, itemType);
  105. if (itemType != ListItemType.Header &&
  106. itemType != ListItemType.Footer) {
  107. switch (ButtonType) {
  108. case ButtonColumnType.LinkButton:
  109. {
  110. LinkButton butt = new LinkButton ();
  111. butt.Text = Text;
  112. butt.CommandName = CommandName;
  113. cell.Controls.Add (butt);
  114. }
  115. break;
  116. case ButtonColumnType.PushButton:
  117. {
  118. Button butt = new Button ();
  119. butt.Text = Text;
  120. butt.CommandName = CommandName;
  121. cell.Controls.Add (butt);
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. protected virtual string FormatDataTextValue (object dataTextValue)
  128. {
  129. if (DataTextFormatString == String.Empty)
  130. return dataTextValue.ToString ();
  131. return String.Format (DataTextFormatString, dataTextValue);
  132. }
  133. }
  134. }