Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. /*
  2. My first gist editing
  3. That's really cool!
  4. */
  5. [ParseChildren(false)]
  6. public class ModernButton : WebControl
  7. {
  8. //Fields
  9. private static readonly object EventClick;
  10. private static readonly object EventCommand;
  11.  
  12. //Events
  13. public event EventHandler Click;
  14. public event CommandEventHandler Command;
  15.  
  16. //Methods
  17. static ModernButton()
  18. {
  19. EventClick = new object();
  20. EventCommand = new object();
  21. }
  22.  
  23. public ModernButton()
  24. : base(HtmlTextWriterTag.Button)
  25. {
  26. }
  27.  
  28. protected override void AddAttributesToRender(HtmlTextWriter writer)
  29. {
  30. PostBackOptions postBackOptions = this.GetPostBackOptions();
  31. string uniqueID = this.UniqueID;
  32. if ((uniqueID != null) && ((postBackOptions == null) || (postBackOptions.TargetControl == this))) {
  33. writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
  34. }
  35. writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value);
  36. bool isEnabled = base.IsEnabled;
  37. string firstScript = String.Empty;
  38. if (isEnabled) {
  39. firstScript = EnsureEndWithSemiColon(this.OnClientClick);
  40. if (base.HasAttributes) {
  41. string baseClick = base.Attributes["onclick"];
  42. if (baseClick != null) {
  43. firstScript += EnsureEndWithSemiColon(baseClick);
  44. base.Attributes.Remove("onclick");
  45. }
  46. }
  47. if (this.Page != null) {
  48. string postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, false);
  49. if (postBackEventReference != null) {
  50. firstScript = MergeScript(firstScript, postBackEventReference);
  51. }
  52. }
  53. }
  54. if (this.Page != null) {
  55. this.Page.ClientScript.RegisterForEventValidation(postBackOptions);
  56. }
  57. if (firstScript.Length > 0) {
  58. writer.AddAttribute(HtmlTextWriterAttribute.Onclick, firstScript);
  59. }
  60. if (this.Enabled && !isEnabled) writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  61. base.AddAttributesToRender(writer);
  62. }
  63.  
  64. protected override void AddParsedSubObject(object obj)
  65. {
  66. if (this.HasControls()) {
  67. base.AddParsedSubObject(obj);
  68. }
  69. else if (obj is LiteralControl) {
  70. this.InnerHtml = ((LiteralControl) obj).Text;
  71. }
  72. else {
  73. string html = this.InnerHtml;
  74. if (html.Length != 0) {
  75. this.InnerHtml = String.Empty;
  76. base.AddParsedSubObject(new LiteralControl(html));
  77. }
  78. }
  79. }
  80.  
  81. private string EnsureEndWithSemiColon(string value)
  82. {
  83. if (value != null) {
  84. int length = value.Length;
  85. if ((length > 0) && (value[length - 1] != ';')) {
  86. return (value + ';');
  87. }
  88. }
  89. return value;
  90. }
  91.  
  92. protected virtual PostBackOptions GetPostBackOptions()
  93. {
  94. PostBackOptions options = new PostBackOptions(this, String.Empty);
  95. options.ClientSubmit = false;
  96. if (this.Page != null) {
  97. if (this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) {
  98. options.PerformValidation = true;
  99. options.ValidationGroup = this.ValidationGroup;
  100. }
  101. if (!String.IsNullOrEmpty(this.PostBackUrl)) {
  102. options.ActionUrl = HttpUtility.UrlPathEncode(base.ResolveClientUrl(this.PostBackUrl));
  103. }
  104. }
  105. return options;
  106. }
  107.  
  108. private string MergeScript(string firstScript, string secondScript)
  109. {
  110. if (!string.IsNullOrEmpty(firstScript)) return (firstScript + secondScript);
  111. if (secondScript.TrimStart(new char[0]).StartsWith("javascript:", StringComparison.Ordinal)) {
  112. return secondScript;
  113. }
  114. return ("javascript:" + secondScript);
  115. }
  116.  
  117. protected void OnClick(EventArgs e)
  118. {
  119. EventHandler handler = (EventHandler) base.Events[EventClick];
  120. if (handler != null) handler(this, e);
  121. }
  122.  
  123. protected void OnCommand(CommandEventArgs e)
  124. {
  125. CommandEventHandler handler = (CommandEventHandler) base.Events[EventCommand];
  126. if (handler != null) handler(this, e);
  127. base.RaiseBubbleEvent(this, e);
  128. }
  129.  
  130. protected override void RenderContents(HtmlTextWriter writer)
  131. {
  132. writer.Write(this.InnerHtml);
  133. }
  134.  
  135. //Properties
  136. public virtual bool CausesValidation
  137. {
  138. get
  139. {
  140. object causesValidation = this.ViewState["CausesValidation"];
  141. if (causesValidation != null) return (bool) causesValidation;
  142. return true;
  143. }
  144. set
  145. {
  146. this.ViewState["CausesValidation"] = value;
  147. }
  148. }
  149.  
  150. public string CommandArgument
  151. {
  152. get
  153. {
  154. return this.ViewState["CommandArgument"] as string ?? String.Empty;
  155. }
  156. set
  157. {
  158. this.ViewState["CommandArgument"] = value;
  159. }
  160. }
  161.  
  162. public string CommandName
  163. {
  164. get
  165. {
  166. return this.ViewState["CommandName"] as string ?? String.Empty;
  167. }
  168. set
  169. {
  170. this.ViewState["CommandName"] = value;
  171. }
  172. }
  173.  
  174. public string InnerHtml
  175. {
  176. get
  177. {
  178. return this.ViewState["InnerHtml"] as string ?? String.Empty;
  179. }
  180. set
  181. {
  182. this.ViewState["InnerHtml"] = value;
  183. }
  184. }
  185.  
  186. public virtual string OnClientClick
  187. {
  188. get
  189. {
  190. return this.ViewState["OnClientClick"] as string ?? String.Empty;
  191. }
  192. set
  193. {
  194. this.ViewState["OnClientClick"] = value;
  195. }
  196. }
  197.  
  198. public virtual string PostBackUrl
  199. {
  200. get
  201. {
  202. return this.ViewState["PostBackUrl"] as string ?? String.Empty;
  203. }
  204. set
  205. {
  206. this.ViewState["PostBackUrl"] = value;
  207. }
  208. }
  209.  
  210. public virtual string ValidationGroup
  211. {
  212. get
  213. {
  214. return this.ViewState["ValidationGroup"] as string ?? String.Empty;
  215. }
  216. set
  217. {
  218. this.ViewState["ValidationGroup"] = value;
  219. }
  220. }
  221.  
  222. public string Value
  223. {
  224. get
  225. {
  226. return this.ViewState["Value"] as string ?? String.Empty;
  227. }
  228. set { this.ViewState["Value"] = value; }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement