EditCommandColumn.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System.ComponentModel;
  27. using System.Security.Permissions;
  28. namespace System.Web.UI.WebControls {
  29. // CAS
  30. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  31. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  32. public class EditCommandColumn : DataGridColumn {
  33. #region Public Constructors
  34. public EditCommandColumn() {
  35. }
  36. #endregion // Public Constructors
  37. #region Public Instance Properties
  38. public virtual ButtonColumnType ButtonType {
  39. get {
  40. object obj;
  41. obj = ViewState["ButtonType"];
  42. if (obj != null) {
  43. return (ButtonColumnType)obj;
  44. }
  45. return ButtonColumnType.LinkButton;
  46. }
  47. set {
  48. ViewState["ButtonType"] = value;
  49. }
  50. }
  51. #if NET_2_0
  52. [DefaultValue ("")]
  53. [Localizable (true)]
  54. #endif
  55. public virtual string CancelText {
  56. get {
  57. return ViewState.GetString("CancelText", string.Empty);
  58. }
  59. set {
  60. ViewState["CancelText"] = value;
  61. }
  62. }
  63. #if NET_2_0
  64. [DefaultValue(true)]
  65. public virtual bool CausesValidation {
  66. get { return ViewState.GetBool ("CausesValidation", true); }
  67. set { ViewState ["CausesValidation"] = value; }
  68. }
  69. [DefaultValue("")]
  70. public virtual string ValidationGroup {
  71. get { return ViewState.GetString ("ValidationGroup", ""); }
  72. set { ViewState ["ValidationGroup"] = value; }
  73. }
  74. #endif
  75. #if NET_2_0
  76. [DefaultValue ("")]
  77. [Localizable (true)]
  78. #endif
  79. public virtual string EditText {
  80. get {
  81. return ViewState.GetString("EditText", string.Empty);
  82. }
  83. set {
  84. ViewState["EditText"] = value;
  85. }
  86. }
  87. #if NET_2_0
  88. [DefaultValue ("")]
  89. [Localizable (true)]
  90. #endif
  91. public virtual string UpdateText {
  92. get {
  93. return ViewState.GetString("UpdateText", string.Empty);
  94. }
  95. set {
  96. ViewState["UpdateText"] = value;
  97. }
  98. }
  99. #endregion // Public Instance Properties
  100. #region Public Instance Methods
  101. // Modeled after Ben's CommandField.InitializeCell. Saved me a lot of figuring-out time :-)
  102. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) {
  103. base.InitializeCell (cell, columnIndex, itemType);
  104. switch(itemType) {
  105. case ListItemType.Separator:
  106. case ListItemType.Pager:
  107. case ListItemType.Footer:
  108. case ListItemType.Header: {
  109. // Base handles header and footer, dunno about the others
  110. return;
  111. }
  112. case ListItemType.Item:
  113. case ListItemType.SelectedItem:
  114. case ListItemType.AlternatingItem:{
  115. cell.Controls.Add(CreateButton(ButtonType, EditText, "Edit", false));
  116. break;
  117. }
  118. case ListItemType.EditItem: {
  119. #if NET_2_0
  120. cell.Controls.Add (CreateButton (ButtonType, UpdateText, "Update", CausesValidation));
  121. #else
  122. cell.Controls.Add(CreateButton(ButtonType, UpdateText, "Update", true));
  123. #endif
  124. cell.Controls.Add(new LiteralControl(" "));
  125. cell.Controls.Add(CreateButton(ButtonType, CancelText, "Cancel", false));
  126. break;
  127. }
  128. }
  129. }
  130. #endregion // Public Instance Methods
  131. #region Private Methods
  132. private Control CreateButton(ButtonColumnType type, string text, string command, bool valid) {
  133. Button b;
  134. LinkButton d;
  135. if (type == ButtonColumnType.LinkButton) {
  136. d = new ForeColorLinkButton();
  137. d.Text = text;
  138. d.CommandName = command;
  139. d.CausesValidation = valid;
  140. #if NET_2_0
  141. if (valid) {
  142. d.ValidationGroup = ValidationGroup;
  143. }
  144. #endif
  145. return d;
  146. }
  147. b = new Button();
  148. b.Text = text;
  149. b.CommandName = command;
  150. b.CausesValidation = valid;
  151. #if NET_2_0
  152. if (valid) {
  153. b.ValidationGroup = ValidationGroup;
  154. }
  155. #endif
  156. return b;
  157. }
  158. #endregion // Private Methods
  159. }
  160. }