DataControlButton.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // System.Web.UI.WebControls.DataControlButton.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (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. #if NET_2_0
  29. using System;
  30. using System.Web;
  31. using System.Web.UI;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. namespace System.Web.UI.WebControls
  35. {
  36. internal class DataControlButton: Button
  37. {
  38. Control container;
  39. public DataControlButton (Control container)
  40. {
  41. this.container = container;
  42. }
  43. public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)
  44. {
  45. this.container = container;
  46. Text = text;
  47. ImageUrl = image;
  48. CommandName = command;
  49. CommandArgument = commandArg;
  50. AllowCallback = allowCallback;
  51. }
  52. public string ImageUrl {
  53. get {
  54. object o = ViewState["iu"];
  55. if (o != null) return (string) o;
  56. return String.Empty;
  57. }
  58. set {
  59. ViewState["iu"] = value;
  60. }
  61. }
  62. public bool AllowCallback {
  63. get {
  64. object o = ViewState["ac"];
  65. if (o != null) return (bool) o;
  66. return true;
  67. }
  68. set {
  69. ViewState["ac"] = value;
  70. }
  71. }
  72. public virtual ButtonType ButtonType {
  73. get {
  74. object ob = ViewState ["ButtonType"];
  75. if (ob != null) return (ButtonType) ob;
  76. return ButtonType.Link;
  77. }
  78. set {
  79. ViewState ["ButtonType"] = value;
  80. }
  81. }
  82. protected internal override void Render (HtmlTextWriter writer)
  83. {
  84. if (CommandName.Length > 0 || ButtonType == ButtonType.Button)
  85. {
  86. string postScript = null;
  87. string callScript = null;
  88. IPostBackContainer pcner = container as IPostBackContainer;
  89. if (pcner != null) {
  90. PostBackOptions ops = pcner.GetPostBackOptions (this);
  91. postScript = container.Page.ClientScript.GetPostBackEventReference (ops);
  92. } else
  93. postScript = Page.ClientScript.GetPostBackEventReference (this, "");
  94. if (CausesValidation && Page.Validators.Count > 0) {
  95. // TOSHOK: review if this is the correct usage of the "fresh" client side stuff
  96. ClientScriptManager csm = new ClientScriptManager (Page);
  97. postScript = csm.GetClientValidationEvent () + postScript;
  98. }
  99. if (AllowCallback) {
  100. ICallbackContainer ccner = container as ICallbackContainer;
  101. if (ccner != null)
  102. callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
  103. }
  104. ControlStyle.AddAttributesToRender (writer);
  105. if (ButtonType == ButtonType.Link || ButtonType == ButtonType.Image)
  106. {
  107. if (ImageUrl.Length > 0) {
  108. writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
  109. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));
  110. if (callScript != null)
  111. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  112. else
  113. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
  114. if (Text.Length > 0)
  115. writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
  116. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  117. writer.RenderEndTag ();
  118. }
  119. else {
  120. if (callScript != null) {
  121. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  122. writer.AddAttribute (HtmlTextWriterAttribute.Href, "javascript:");
  123. }
  124. else
  125. writer.AddAttribute (HtmlTextWriterAttribute.Href, "javascript:" + postScript);
  126. writer.RenderBeginTag (HtmlTextWriterTag.A);
  127. writer.Write (Text);
  128. writer.RenderEndTag ();
  129. }
  130. }
  131. else if (ButtonType == ButtonType.Button)
  132. {
  133. if (callScript != null)
  134. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  135. else
  136. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
  137. writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
  138. writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
  139. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  140. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  141. writer.RenderEndTag ();
  142. }
  143. } else {
  144. if (ImageUrl.Length > 0) {
  145. ControlStyle.AddAttributesToRender (writer);
  146. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));
  147. if (Text.Length > 0)
  148. writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
  149. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  150. writer.RenderEndTag ();
  151. }
  152. else {
  153. if (!ControlStyle.IsEmpty) {
  154. ControlStyle.AddAttributesToRender (writer);
  155. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  156. writer.Write (Text);
  157. writer.RenderEndTag ();
  158. } else
  159. writer.Write (Text);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. #endif