PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MvcFutures/Mvc/ButtonsAndLinkExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 152 lines | 58 code | 13 blank | 81 comment | 2 complexity | 73237042daa188c1e0a85be53175dd2c MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  8. public static class ButtonsAndLinkExtensions {
  9. /// <summary>
  10. /// Creates a submit button for your form
  11. /// </summary>
  12. /// <param name="buttonText">The text for the button face</param>
  13. public static string SubmitButton(this HtmlHelper helper, string name) {
  14. return SubmitButton(helper, name, null, (IDictionary<string, object>)null);
  15. }
  16. /// <summary>
  17. /// Creates a submit button for your form
  18. /// </summary>
  19. /// <param name="buttonText">The text for the button face</param>
  20. public static string SubmitButton(this HtmlHelper helper, string name, string buttonText) {
  21. return SubmitButton(helper, name, buttonText, null);
  22. }
  23. /// <summary>
  24. /// Creates a submit button for your form
  25. /// </summary>
  26. public static string SubmitButton(this HtmlHelper helper) {
  27. return SubmitButton(helper, null, null, (IDictionary<string, object>)null);
  28. }
  29. /// <summary>
  30. /// Creates a submit button for your form
  31. /// </summary>
  32. /// <param name="name">Name of the button</param>
  33. /// <param name="buttonText">The text for the button face</param>
  34. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  35. /// <returns></returns>
  36. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "helper", Justification = "Required for Extension Method")]
  37. public static string SubmitButton(this HtmlHelper helper, string name, string buttonText, object htmlAttributes) {
  38. return helper.SubmitButton(name, buttonText, new RouteValueDictionary(htmlAttributes));
  39. }
  40. /// <summary>
  41. /// Creates a submit button for your form
  42. /// </summary>
  43. /// <param name="name">Name of the button</param>
  44. /// <param name="buttonText">The text for the button face</param>
  45. /// <param name="htmlAttributes">Dictionary of HTML settings</param>
  46. /// <returns></returns>
  47. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "helper", Justification = "Required for Extension Method")]
  48. public static string SubmitButton(this HtmlHelper helper, string name, string buttonText, IDictionary<string, object> htmlAttributes) {
  49. return ButtonBuilder.SubmitButton(name, buttonText, htmlAttributes).ToString(TagRenderMode.SelfClosing);
  50. }
  51. /// <summary>
  52. /// Creates a submit button for your form using an image
  53. /// </summary>
  54. /// <param name="name">Name of the button</param>
  55. /// <param name="imageSrc">The URL for the image</param>
  56. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  57. /// <returns></returns>
  58. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  59. public static string SubmitImage(this HtmlHelper helper, string name, string imageSrc) {
  60. return helper.SubmitImage(name, imageSrc, null);
  61. }
  62. /// <summary>
  63. /// Creates a submit button for your form using an image
  64. /// </summary>
  65. /// <param name="name">Name of the button</param>
  66. /// <param name="imageSrc">The URL for the image</param>
  67. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  68. /// <returns></returns>
  69. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  70. public static string SubmitImage(this HtmlHelper helper, string name, string imageSrc, object htmlAttributes) {
  71. return helper.SubmitImage(name, imageSrc, new RouteValueDictionary(htmlAttributes));
  72. }
  73. /// <summary>
  74. /// Creates a submit button for your form using an image
  75. /// </summary>
  76. /// <param name="htmlName">Name of the button</param>
  77. /// <param name="imageSrc">The URL for the image</param>
  78. /// <param name="htmlAttributes">Dictionary of HTML settings</param>
  79. /// <returns></returns>
  80. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  81. public static string SubmitImage(this HtmlHelper helper, string name, string imageSrc, IDictionary<string, object> htmlAttributes) {
  82. if (imageSrc == null) {
  83. throw new ArgumentNullException("imageSrc");
  84. }
  85. UrlHelper urlHelper = new UrlHelper(helper.ViewContext); // TODO: Why do I need to do this?
  86. string resolvedUrl = urlHelper.Content(imageSrc);
  87. return ButtonBuilder.SubmitImage(name, resolvedUrl, htmlAttributes).ToString(TagRenderMode.SelfClosing);
  88. }
  89. /// <summary>
  90. /// A Simple button you can use with javascript
  91. /// </summary>
  92. /// <param name="name">Name of the button</param>
  93. /// <param name="buttonText">The text for the button face</param>
  94. /// <param name="buttonType">The button type (Button, Submit, or Reset)</param>
  95. /// <param name="onClickMethod">The method or script routine to call when the button is clicked.</param>
  96. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  97. /// <returns></returns>
  98. public static string Button(this HtmlHelper helper, string name, string buttonText, HtmlButtonType buttonType) {
  99. return helper.Button(name, buttonText, buttonType, null, (IDictionary<string, object>)null);
  100. }
  101. /// <summary>
  102. /// A Simple button you can use with javascript
  103. /// </summary>
  104. /// <param name="name">Name of the button</param>
  105. /// <param name="buttonText">The text for the button face</param>
  106. /// <param name="buttonType">The button type (Button, Submit, or Reset)</param>
  107. /// <param name="onClickMethod">The method or script routine to call when the button is clicked.</param>
  108. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  109. /// <returns></returns>
  110. public static string Button(this HtmlHelper helper, string name, string buttonText, HtmlButtonType buttonType, string onClickMethod) {
  111. return helper.Button(name, buttonText, buttonType, onClickMethod, (IDictionary<string, object>)null);
  112. }
  113. /// <summary>
  114. /// A Simple button you can use with javascript
  115. /// </summary>
  116. /// <param name="name">Name of the button</param>
  117. /// <param name="buttonText">The text for the button face</param>
  118. /// <param name="buttonType">The button type (Button, Submit, or Reset)</param>
  119. /// <param name="onClickMethod">The method or script routine to call when the button is clicked.</param>
  120. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  121. /// <returns></returns>
  122. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "helper", Justification = "This is an Extension Method and requires this argument")]
  123. public static string Button(this HtmlHelper helper, string name, string buttonText, HtmlButtonType buttonType, string onClickMethod, object htmlAttributes) {
  124. return helper.Button(name, buttonText, buttonType, onClickMethod, new RouteValueDictionary(htmlAttributes));
  125. }
  126. /// <summary>
  127. /// A Simple button you can use with javascript
  128. /// </summary>
  129. /// <param name="name">Name of the button</param>
  130. /// <param name="buttonText">The text for the button face</param>
  131. /// <param name="onClickMethod">The method or script routine to call when the button is clicked.</param>
  132. /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
  133. /// <returns></returns>
  134. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "helper", Justification = "This is an Extension Method and requires this argument")]
  135. public static string Button(this HtmlHelper helper, string name, string buttonText, HtmlButtonType buttonType, string onClickMethod, IDictionary<string, object> htmlAttributes) {
  136. return ButtonBuilder.Button(name, buttonText, buttonType, onClickMethod, htmlAttributes).ToString(TagRenderMode.Normal);
  137. }
  138. }
  139. }