TextBoxValueResolver.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Web.Controls.Mvc
  6. {
  7. public abstract class TextBoxValueResolver
  8. {
  9. public abstract string Execute(TextBox txtBox);
  10. public static TextBoxValueResolver CreateValueResolver(TextBoxType type)
  11. {
  12. TextBoxValueResolver resolver = new DefaultTextBoxValueResolver();
  13. switch (type)
  14. {
  15. case TextBoxType.DateTime:
  16. resolver = new DateTimeTextBoxValueResolver();
  17. break;
  18. case TextBoxType.Date:
  19. resolver = new DateTextBoxValueResolver();
  20. break;
  21. case TextBoxType.Time:
  22. resolver = new TimeTextBoxValueResolver();
  23. break;
  24. default:
  25. break;
  26. }
  27. return resolver;
  28. }
  29. }
  30. public class DefaultTextBoxValueResolver:TextBoxValueResolver
  31. {
  32. public override string Execute(TextBox txtBox)
  33. {
  34. return txtBox.Value == null ? "" : txtBox.Value.ToString();
  35. }
  36. }
  37. internal class DateTextBoxValueResolver : TextBoxValueResolver
  38. {
  39. public override string Execute(TextBox txtBox)
  40. {
  41. return txtBox.Value == null ? "" : (Convert.ToDateTime(txtBox.Value)).ToString("yyyy-MM-dd");
  42. }
  43. }
  44. internal class DateTimeTextBoxValueResolver : TextBoxValueResolver
  45. {
  46. public override string Execute(TextBox txtBox)
  47. {
  48. return txtBox.Value == null ? "" : (Convert.ToDateTime(txtBox.Value)).ToString("yyyy-MM-dd HH:mm:ss");
  49. }
  50. }
  51. internal class TimeTextBoxValueResolver : TextBoxValueResolver
  52. {
  53. public override string Execute(TextBox txtBox)
  54. {
  55. if (txtBox.Value is TimeSpan)
  56. {
  57. return txtBox.Value == null ? "" : ((TimeSpan)txtBox.Value).ToString("hh\\:mm\\:ss");
  58. }
  59. else
  60. {
  61. return txtBox.Value == null ? "" : (Convert.ToDateTime(txtBox.Value)).ToString("HH:mm:ss");
  62. }
  63. }
  64. }
  65. }