PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/MVC/src/MvcFutures/Mvc/ButtonsAndLinkExtensions.cs

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