CommandField.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // System.Web.UI.WebControls.CommandField.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.Collections;
  31. using System.Collections.Specialized;
  32. using System.Web.UI;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. namespace System.Web.UI.WebControls {
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public class CommandField : ButtonFieldBase
  39. {
  40. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  41. [WebCategoryAttribute ("Appearance")]
  42. [UrlPropertyAttribute]
  43. [DefaultValueAttribute ("")]
  44. public virtual string CancelImageUrl {
  45. get {
  46. object ob = ViewState ["CancelImageUrl"];
  47. if (ob != null) return (string) ob;
  48. return "";
  49. }
  50. set {
  51. ViewState ["CancelImageUrl"] = value;
  52. OnFieldChanged ();
  53. }
  54. }
  55. [WebCategoryAttribute ("Appearance")]
  56. [LocalizableAttribute (true)]
  57. public virtual string CancelText {
  58. get {
  59. object ob = ViewState ["CancelText"];
  60. if (ob != null) return (string) ob;
  61. return "Cancel";
  62. }
  63. set {
  64. ViewState ["CancelText"] = value;
  65. OnFieldChanged ();
  66. }
  67. }
  68. [DefaultValueAttribute (true)]
  69. public override bool CausesValidation {
  70. get {
  71. object ob = ViewState ["CausesValidation"];
  72. if (ob != null) return (bool) ob;
  73. return true;
  74. }
  75. set {
  76. ViewState ["CausesValidation"] = value;
  77. OnFieldChanged ();
  78. }
  79. }
  80. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  81. [WebCategoryAttribute ("Appearance")]
  82. [UrlPropertyAttribute]
  83. [DefaultValueAttribute ("")]
  84. public virtual string DeleteImageUrl {
  85. get {
  86. object ob = ViewState ["DeleteImageUrl"];
  87. if (ob != null) return (string) ob;
  88. return "";
  89. }
  90. set {
  91. ViewState ["DeleteImageUrl"] = value;
  92. OnFieldChanged ();
  93. }
  94. }
  95. [WebCategoryAttribute ("Appearance")]
  96. [LocalizableAttribute (true)]
  97. public virtual string DeleteText {
  98. get {
  99. object ob = ViewState ["DeleteText"];
  100. if (ob != null) return (string) ob;
  101. return "Delete";
  102. }
  103. set {
  104. ViewState ["DeleteText"] = value;
  105. OnFieldChanged ();
  106. }
  107. }
  108. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  109. [WebCategoryAttribute ("Appearance")]
  110. [UrlPropertyAttribute]
  111. [DefaultValueAttribute ("")]
  112. public virtual string EditImageUrl {
  113. get {
  114. object ob = ViewState ["EditImageUrl"];
  115. if (ob != null) return (string) ob;
  116. return "";
  117. }
  118. set {
  119. ViewState ["EditImageUrl"] = value;
  120. OnFieldChanged ();
  121. }
  122. }
  123. [WebCategoryAttribute ("Appearance")]
  124. [LocalizableAttribute (true)]
  125. public virtual string EditText {
  126. get {
  127. object ob = ViewState ["EditText"];
  128. if (ob != null) return (string) ob;
  129. return "Edit";
  130. }
  131. set {
  132. ViewState ["EditText"] = value;
  133. OnFieldChanged ();
  134. }
  135. }
  136. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  137. [WebCategoryAttribute ("Appearance")]
  138. [UrlPropertyAttribute]
  139. [DefaultValueAttribute ("")]
  140. public virtual string InsertImageUrl {
  141. get {
  142. object ob = ViewState ["InsertImageUrl"];
  143. if (ob != null) return (string) ob;
  144. return "";
  145. }
  146. set {
  147. ViewState ["InsertImageUrl"] = value;
  148. OnFieldChanged ();
  149. }
  150. }
  151. [WebCategoryAttribute ("Appearance")]
  152. [LocalizableAttribute (true)]
  153. public virtual string InsertText {
  154. get {
  155. object ob = ViewState ["InsertText"];
  156. if (ob != null) return (string) ob;
  157. return "Insert";
  158. }
  159. set {
  160. ViewState ["InsertText"] = value;
  161. OnFieldChanged ();
  162. }
  163. }
  164. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  165. [WebCategoryAttribute ("Appearance")]
  166. [UrlPropertyAttribute]
  167. [DefaultValueAttribute ("")]
  168. public virtual string NewImageUrl {
  169. get {
  170. object ob = ViewState ["NewImageUrl"];
  171. if (ob != null) return (string) ob;
  172. return "";
  173. }
  174. set {
  175. ViewState ["NewImageUrl"] = value;
  176. OnFieldChanged ();
  177. }
  178. }
  179. [WebCategoryAttribute ("Appearance")]
  180. [LocalizableAttribute (true)]
  181. public virtual string NewText {
  182. get {
  183. object ob = ViewState ["NewText"];
  184. if (ob != null) return (string) ob;
  185. return "New";
  186. }
  187. set {
  188. ViewState ["NewText"] = value;
  189. OnFieldChanged ();
  190. }
  191. }
  192. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  193. [WebCategoryAttribute ("Appearance")]
  194. [UrlPropertyAttribute]
  195. [DefaultValueAttribute ("")]
  196. public virtual string SelectImageUrl {
  197. get {
  198. object ob = ViewState ["SelectImageUrl"];
  199. if (ob != null) return (string) ob;
  200. return "";
  201. }
  202. set {
  203. ViewState ["SelectImageUrl"] = value;
  204. OnFieldChanged ();
  205. }
  206. }
  207. [WebCategoryAttribute ("Appearance")]
  208. [LocalizableAttribute (true)]
  209. public virtual string SelectText {
  210. get {
  211. object ob = ViewState ["SelectText"];
  212. if (ob != null) return (string) ob;
  213. return "Select";
  214. }
  215. set {
  216. ViewState ["SelectText"] = value;
  217. OnFieldChanged ();
  218. }
  219. }
  220. [DefaultValueAttribute (true)]
  221. [WebCategoryAttribute ("Behavior")]
  222. public virtual bool ShowCancelButton {
  223. get {
  224. object ob = ViewState ["ShowCancelButton"];
  225. if (ob != null) return (bool) ob;
  226. return true;
  227. }
  228. set {
  229. ViewState ["ShowCancelButton"] = value;
  230. OnFieldChanged ();
  231. }
  232. }
  233. [DefaultValueAttribute (false)]
  234. [WebCategoryAttribute ("Behavior")]
  235. public virtual bool ShowDeleteButton {
  236. get {
  237. object ob = ViewState ["ShowDeleteButton"];
  238. if (ob != null) return (bool) ob;
  239. return false;
  240. }
  241. set {
  242. ViewState ["ShowDeleteButton"] = value;
  243. OnFieldChanged ();
  244. }
  245. }
  246. [DefaultValueAttribute (false)]
  247. [WebCategoryAttribute ("Behavior")]
  248. public virtual bool ShowEditButton {
  249. get {
  250. object ob = ViewState ["ShowEditButton"];
  251. if (ob != null) return (bool) ob;
  252. return false;
  253. }
  254. set {
  255. ViewState ["ShowEditButton"] = value;
  256. OnFieldChanged ();
  257. }
  258. }
  259. [DefaultValueAttribute (false)]
  260. [WebCategoryAttribute ("Behavior")]
  261. public virtual bool ShowSelectButton {
  262. get {
  263. object ob = ViewState ["ShowSelectButton"];
  264. if (ob != null) return (bool) ob;
  265. return false;
  266. }
  267. set {
  268. ViewState ["ShowSelectButton"] = value;
  269. OnFieldChanged ();
  270. }
  271. }
  272. [DefaultValueAttribute (false)]
  273. [WebCategoryAttribute ("Behavior")]
  274. public virtual bool ShowInsertButton {
  275. get {
  276. object ob = ViewState ["ShowInsertButton"];
  277. if (ob != null) return (bool) ob;
  278. return false;
  279. }
  280. set {
  281. ViewState ["ShowInsertButton"] = value;
  282. OnFieldChanged ();
  283. }
  284. }
  285. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  286. [WebCategoryAttribute ("Appearance")]
  287. [UrlPropertyAttribute]
  288. [DefaultValueAttribute ("")]
  289. public virtual string UpdateImageUrl {
  290. get {
  291. object ob = ViewState ["UpdateImageUrl"];
  292. if (ob != null) return (string) ob;
  293. return "";
  294. }
  295. set {
  296. ViewState ["UpdateImageUrl"] = value;
  297. OnFieldChanged ();
  298. }
  299. }
  300. [WebCategoryAttribute ("Appearance")]
  301. [LocalizableAttribute (true)]
  302. public virtual string UpdateText {
  303. get {
  304. object ob = ViewState ["UpdateText"];
  305. if (ob != null) return (string) ob;
  306. return "Update";
  307. }
  308. set {
  309. ViewState ["UpdateText"] = value;
  310. OnFieldChanged ();
  311. }
  312. }
  313. public override void InitializeCell (DataControlFieldCell cell,
  314. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  315. {
  316. string index = rowIndex.ToString ();
  317. if (cellType == DataControlCellType.DataCell)
  318. {
  319. if ((rowState & DataControlRowState.Edit) != 0) {
  320. cell.Controls.Add (CreateButton (UpdateText, UpdateImageUrl, DataControlCommands.UpdateCommandName, index));
  321. if (ShowCancelButton) {
  322. AddSeparator (cell);
  323. cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
  324. }
  325. } else if ((rowState & DataControlRowState.Insert) != 0) {
  326. cell.Controls.Add (CreateButton (InsertText, InsertImageUrl, DataControlCommands.InsertCommandName, index));
  327. if (ShowCancelButton) {
  328. AddSeparator (cell);
  329. cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
  330. }
  331. } else {
  332. if (ShowEditButton) {
  333. AddSeparator (cell);
  334. cell.Controls.Add (CreateButton (EditText, EditImageUrl, DataControlCommands.EditCommandName, index));
  335. }
  336. if (ShowDeleteButton) {
  337. AddSeparator (cell);
  338. cell.Controls.Add (CreateButton (DeleteText, DeleteImageUrl, DataControlCommands.DeleteCommandName, index));
  339. }
  340. if (ShowSelectButton) {
  341. AddSeparator (cell);
  342. cell.Controls.Add (CreateButton (SelectText, SelectImageUrl, DataControlCommands.SelectCommandName, index));
  343. }
  344. if (ShowInsertButton) {
  345. AddSeparator (cell);
  346. cell.Controls.Add (CreateButton (NewText, NewImageUrl, DataControlCommands.NewCommandName, index));
  347. }
  348. }
  349. } else
  350. base.InitializeCell (cell, cellType, rowState, rowIndex);
  351. }
  352. DataControlButton CreateButton (string text, string image, string command, string arg)
  353. {
  354. DataControlButton c = new DataControlButton (Control, text, image, command, arg, false);
  355. c.CausesValidation = CausesValidation;
  356. return c;
  357. }
  358. void AddSeparator (DataControlFieldCell cell)
  359. {
  360. if (cell.Controls.Count > 0) {
  361. Literal lit = new Literal ();
  362. lit.Text = " ";
  363. cell.Controls.Add (lit);
  364. }
  365. }
  366. protected override DataControlField CreateField ()
  367. {
  368. return new CommandField ();
  369. }
  370. protected override void CopyProperties (DataControlField newField)
  371. {
  372. base.CopyProperties (newField);
  373. CommandField field = (CommandField) newField;
  374. field.CancelImageUrl = CancelImageUrl;
  375. field.CancelText = CancelText;
  376. field.DeleteImageUrl = DeleteImageUrl;
  377. field.DeleteText = DeleteText;
  378. field.EditImageUrl = EditImageUrl;
  379. field.EditText = EditText;
  380. field.InsertImageUrl = InsertImageUrl;
  381. field.InsertText = InsertText;
  382. field.NewImageUrl = NewImageUrl;
  383. field.NewText = NewText;
  384. field.SelectImageUrl = SelectImageUrl;
  385. field.SelectText = SelectText;
  386. field.ShowCancelButton = ShowCancelButton;
  387. field.ShowDeleteButton = ShowDeleteButton;
  388. field.ShowEditButton = ShowEditButton;
  389. field.ShowSelectButton = ShowSelectButton;
  390. field.ShowInsertButton = ShowInsertButton;
  391. field.UpdateImageUrl = UpdateImageUrl;
  392. field.UpdateText = UpdateText;
  393. }
  394. public override void ValidateSupportsCallback ()
  395. {
  396. }
  397. }
  398. }
  399. #endif