DataControlButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. using System.Drawing;
  35. namespace System.Web.UI.WebControls
  36. {
  37. internal class DataControlButton: Button
  38. {
  39. Control container;
  40. public DataControlButton (Control container)
  41. {
  42. this.container = container;
  43. CausesValidation = false;
  44. }
  45. public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)
  46. : this (container)
  47. {
  48. Text = text;
  49. ImageUrl = image;
  50. CommandName = command;
  51. CommandArgument = commandArg;
  52. AllowCallback = allowCallback;
  53. }
  54. public Control Container {
  55. get { return container; }
  56. set { container = value; }
  57. }
  58. public string ImageUrl {
  59. get {
  60. object o = ViewState["iu"];
  61. if (o != null) return (string) o;
  62. return String.Empty;
  63. }
  64. set {
  65. ViewState["iu"] = value;
  66. }
  67. }
  68. public bool AllowCallback {
  69. get {
  70. object o = ViewState["ac"];
  71. if (o != null) return (bool) o;
  72. return true;
  73. }
  74. set {
  75. ViewState["ac"] = value;
  76. }
  77. }
  78. public virtual ButtonType ButtonType {
  79. get {
  80. object ob = ViewState ["ButtonType"];
  81. if (ob != null) return (ButtonType) ob;
  82. return ButtonType.Link;
  83. }
  84. set {
  85. ViewState ["ButtonType"] = value;
  86. }
  87. }
  88. protected internal override void Render (HtmlTextWriter writer)
  89. {
  90. if (CommandName.Length > 0 && ButtonType == ButtonType.Link) {
  91. EnsureForeColor ();
  92. }
  93. if (CommandName.Length > 0 || CommandArgument.Length > 0 || ButtonType == ButtonType.Button)
  94. {
  95. string postScript = null;
  96. string callScript = null;
  97. PostBackOptions ops = null;
  98. IPostBackContainer pcner = container as IPostBackContainer;
  99. if (pcner != null) {
  100. ops = pcner.GetPostBackOptions (this);
  101. ops.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
  102. }
  103. else
  104. ops = GetPostBackOptions ();
  105. postScript = Page.ClientScript.GetPostBackEventReference (ops, !Page.IsCallback);
  106. if (AllowCallback) {
  107. ICallbackContainer ccner = container as ICallbackContainer;
  108. if (ccner != null)
  109. callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
  110. }
  111. ControlStyle.AddAttributesToRender (writer);
  112. if (ButtonType == ButtonType.Image) {
  113. writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
  114. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
  115. if (callScript != null)
  116. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  117. else
  118. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
  119. if (Text.Length > 0)
  120. writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
  121. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
  122. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  123. writer.RenderEndTag ();
  124. }
  125. if (ButtonType == ButtonType.Link) {
  126. if (callScript != null) {
  127. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  128. }
  129. writer.AddAttribute (HtmlTextWriterAttribute.Href, postScript);
  130. writer.RenderBeginTag (HtmlTextWriterTag.A);
  131. writer.Write (Text);
  132. writer.RenderEndTag ();
  133. }
  134. if (ButtonType == ButtonType.Button) {
  135. if (callScript != null)
  136. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
  137. else
  138. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
  139. writer.AddAttribute (HtmlTextWriterAttribute.Type, "button");
  140. writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
  141. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  142. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  143. writer.RenderEndTag ();
  144. }
  145. }
  146. else {
  147. if (ImageUrl.Length > 0) {
  148. ControlStyle.AddAttributesToRender (writer);
  149. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
  150. if (Text.Length > 0)
  151. writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
  152. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
  153. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  154. writer.RenderEndTag ();
  155. }
  156. else {
  157. if (!ControlStyle.IsEmpty) {
  158. ControlStyle.AddAttributesToRender (writer);
  159. }
  160. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  161. writer.Write (Text);
  162. writer.RenderEndTag ();
  163. }
  164. }
  165. }
  166. protected override PostBackOptions GetPostBackOptions () {
  167. PostBackOptions options = new PostBackOptions (this);
  168. options.Argument = "";
  169. options.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
  170. options.ClientSubmit = true;
  171. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  172. if (options.PerformValidation)
  173. options.ValidationGroup = ValidationGroup;
  174. return options;
  175. }
  176. private void EnsureForeColor () {
  177. if (ForeColor != Color.Empty)
  178. return;
  179. for (Control parent = Parent; parent != null; parent = parent.Parent) {
  180. WebControl wc = parent as WebControl;
  181. if (wc != null && wc.ForeColor != Color.Empty) {
  182. ForeColor = wc.ForeColor;
  183. break;
  184. }
  185. if (parent == container)
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. #endif