Translate

Thursday, 1 September 2016

Disable Datepicker dates based on database list using JQUERY



To disable datapicker date getting from db shown below

Getting dates list from db using ajax call

  $(document).ready(function () {
            $("#ContentPlaceHolder_Body_ddlBlock").change(function () {
                getdates();
            });

            var holidays;
            function getdates() {
                $.ajax({
                    type: "POST",
                    url: "Dates.aspx/GetDatesArray",
                    data: '',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        holidays = data.d;
                        localStorage.setItem("Holidays", holidays);
                    },
                });
            }

            var holidaysList = localStorage.getItem("Holidays");
            if (holidaysList != null && holidaysList != "") {
             var holidays = holidaysList.toString().split(',');
                $("# txtDate").datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'yy-mm-dd',
                    beforeShowDay: function (date) {
                        show = true;
                        if (date.getDay() == 0) { show = false; }//No Weekends
                        for (var i = 0; i < holidays.length; i++) {
                            if (new Date(holidays[i]).toString() == date.toString()) { show = false; }//No Holidays
                        }
                        var display = [show, '', (show) ? '' : 'No Weekends or Holidays'];//With Fancy hover tooltip!
                        return display;
                    }

                });
            }
            else {
                getdates();
            }


        });

webmethod in .cs file




[WebMethod]
        public static List<string> GetDatesArray()
        {
            string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            try
            {
                DataSet Sqlds = new DataSet();
                using (SqlConnection sqlCon = new SqlConnection(consString))
                {
                    using (SqlCommand sqlCmd = new SqlCommand("select * from table", sqlCon))
                    {

                        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
                        sqlDa.Fill(Sqlds);

                    }
                }
                List<string> strPH = new List<string>();

                foreach (DataRow row in Sqlds.Tables[0].Rows)
                {

                    DateTime Dt = Convert.ToDateTime(row["d"].ToString());
                    //strPH.Add(String.Format("{0:MM/dd/yyyy}", Dt));

                    strPH.Add((Dt.ToString("M/d/yyyy")).Replace("-", "/"));
                }

                return strPH;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }