WebDateTimePicker.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using FastReport.Dialog;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. namespace FastReport.Web
  10. {
  11. public partial class WebReport : WebControl, INamingContainer
  12. {
  13. /// <summary>
  14. /// Gets or sets date time format in jqueryui datepicker style
  15. /// </summary>
  16. [DefaultValue(WebReportProperties.DEFAULT_DATE_TIME_PICKER_FORMAT)]
  17. [Category("Data")]
  18. [Browsable(true)]
  19. public string DateTimePickerFormat
  20. {
  21. get { return Prop.DateTimePickerFormat; }
  22. set { Prop.DateTimePickerFormat = value; }
  23. }
  24. private void DateTimePickerChange(DateTimePickerControl dp, string value)
  25. {
  26. dp.Value = DateTime.ParseExact(value, "d", CultureInfo.InvariantCulture);
  27. }
  28. private string GetDateTimePickerHtml(DateTimePickerControl control)
  29. {
  30. control.FillData();
  31. ControlFilterRefresh(control);
  32. string id = Prop.ControlID + control.Name;
  33. StringBuilder html = new StringBuilder();
  34. if(DateTimePickerFormat == null || DateTimePickerFormat == "" || DateTimePickerFormat == WebReportProperties.DEFAULT_DATE_TIME_PICKER_FORMAT)
  35. {
  36. string s = control.Value.Month.ToString() + "/" + control.Value.Day.ToString() + "/" + control.Value.Year.ToString();
  37. string ev = GetEvent("onchange", control, string.Format("document.getElementById('{0}').value", id));
  38. html.AppendFormat("<input type=\"text\" value=\"{4}\" class=\"{0}\" style=\"{1}\" onchange=\"{2}\" id=\"{3}\" {5}/>",
  39. "",
  40. GetDateTimePickerStyle(control),
  41. ev,
  42. id,
  43. s,
  44. control.Enabled ? "" : "disabled"
  45. );
  46. html.Append("<script>$(function() {$( \"#").Append(id).Append("\" ).datepicker();");
  47. html.Append("$( \"#").Append(id).Append("\" ).datepicker( \"option\", \"dateFormat\", \"").
  48. Append(WebReportProperties.DEFAULT_DATE_TIME_PICKER_FORMAT).Append("\" );");
  49. html.Append("});</script>");
  50. }
  51. else
  52. {
  53. string value = "(function(){{ var tStr = function(k){{ if( k < 10) return '0' + k; return k; }}; var dateTime=$('#{0}').datepicker('getDate'); if(dateTime) return tStr(dateTime.getMonth() + 1 ) + '/' + tStr(dateTime.getDate()) + '/' + tStr(dateTime.getFullYear()); return '01/01/2019';}})()";
  54. string ev = GetEvent("onchange", control, string.Format(value, id));
  55. html.AppendFormat("<input type=\"text\" class=\"{0}\" style=\"{1}\" onchange=\"{2}\" id=\"{3}\"/>",
  56. "",
  57. GetDateTimePickerStyle(control),
  58. ev,
  59. id
  60. );
  61. html.Append("<script>$(function() {$( \"#").Append(id).Append("\" ).datepicker();");
  62. html.Append("$( \"#").Append(id).Append("\" ).datepicker( \"option\", \"dateFormat\", \"").
  63. Append(DateTimePickerFormat).Append("\" );");
  64. html.Append("$( \"#").Append(id).Append("\" ).datepicker( \"setDate\", new Date( ")
  65. .Append(control.Value.Year).Append(",").Append(control.Value.Month - 1).Append(",").Append(control.Value.Day)
  66. .Append("));");
  67. html.Append("});</script>");
  68. }
  69. //control.FilterData();
  70. return html.ToString();
  71. }
  72. private string GetDateTimePickerStyle(DateTimePickerControl control)
  73. {
  74. return GetStandardStyle(control);
  75. }
  76. }
  77. }