将日期列表传递给JQuery日历

时间:2013-02-22 17:05:00

标签: jquery asp.net list datetime jquery-ui-datepicker

有没有人知道如何将代码中的日期列表传递给日历,因此它会突出显示列表中的日期。谢谢

                                   

    <script>
        $(function () {
        $("#datepicker").datepicker();
        });
    </script>

   <p>Date: <input type="text" id="datepicker" /></p>

后面代码中存储的日期列表:

DateTime start = new DateTime(2013, 02, 20);
        DateTime end = new DateTime(2013, 01, 11);

        var dates = new List<DateTime>();

        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
            dates.Add(dt);
        }

1 个答案:

答案 0 :(得分:2)

最简单的解决方案是创建asp.net处理程序或将日期作为json对象返回的网页,并在datepicker内的beforeShowDate函数中使用它:

以下是简单的实现:

var dates = []; //replace [] with ajax call to the page that returns the dates
$("#datepicker").datepicker({
      dateFormat: "dd-mm-yyyy",

      beforeShowDay: function (date) {
          var available = $.inArray(date, dates) > -1;   

          if (available) {
              return [1];   //return available date
          }
          else {
              return [0];   //return not available
          }
      }
});

背后的代码看起来像这样。您必须添加System.Web.Extensions.dll作为参考才能使用JavaScriptSerializer

    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime start = new DateTime(2013, 02, 20);
        DateTime end = new DateTime(2014, 01, 11);

        var dates = new List<string>();

        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
            dates.Add(dt.ToString("dd-MM-yyyy"));
        }

        Response.ContentType = "text/javascript";
        string json = new JavaScriptSerializer().Serialize(dates);
        Response.Write(json);
        Response.End();
    }`