How to set maxDate (jquery-ui datepicker) from a asp.net textbox?

Posted on 2014-08-11
Medium Priority
Last Modified: 2014-10-28
I have a datepicker (Jquery-UI) that works fine if I set maxDate with literal numbers as in

 $(document).ready(function () {
        $('#<%= SearchToDateTextBox.ClientID %>').datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: "mm/dd/yyyy",
                maxDate: new Date(2013, 3, 21)

Open in new window

The information for new Date(2013,3,21) is 04/21/2013 in an asp.net textbox on screen and is filled in by an SQL Connection. I want that date to be the maxDate for the datepicker representing the To date: and the defaultDate for the From Date:

I've tried getting values using document.getElementById("ToDate").valueOf directly and also by creating var as intermediate steps.

maxDate is fairly useless to me unless I can control it.

Question by:bob_mechler
LVL 25

Expert Comment

by:Luis Pérez
ID: 40255457
So, do you have 2 fields called "ToDate" and "FromDate" in your form? If so, what kind of controls are them and how is the date displayed in them -which format-?
LVL 30

Accepted Solution

Alexandre Simões earned 2000 total points
ID: 40255465
You can set the maxDate with whatever you want at any time.
See the documentation here: http://api.jqueryui.com/datepicker/#option-maxDate
And the date format should comply to the specified here: http://api.jqueryui.com/datepicker/#option-dateFormat

To get the current date on the datepicker you can use this:

Author Comment

ID: 40257507
The web page produces an RDLC report. The purpose of the From date: and To date: is to pass a date range to the report.

            $(document).ready(function () {
             $('#<%= SearchToDateTextBox.ClientID %>').datepicker({
                 changeMonth: true,
                 changeYear: true,
                 dateFormat: "mm/dd/yyyy",
                 maxDate: "<%= DefaultToDate%>"

Open in new window

'#<%= SearchToDateTextBox.ClientID %>' is the field that the datepicker is supposed to be tied to.
"<%= DefaultToDate%>" is a date from the code behind that represents the most recent posting date for the users investment account. It first comes into a dataset and then I place it in a protected DateTime variable in the c# code behind.

        protected DateTime DefaultToDate

 DataSet sysDat = ReportsModel.GetSysDat(Convert.ToInt32(Session["SessionID"].ToString()), Session["IPAddress"].ToString());
            DefaultToDate = Convert.ToDateTime(sysDat.Tables["SysDat"].Rows[0][0].ToString());
            SearchToDateTextBox.Text = DefaultToDate.ToString("MM/dd/yyyy");

Open in new window

The first line of code creates a protected DateTime variable that is supposed to be available to JavaScript using the notation in the code block before this one.
The date is then put in the .SearchToDateTextBox.Text .
The correct date ends up in the textbox properly but the datepicker calendar shows the current date which could be a month apart. It only sync's up if I erase the value in the textbox and type it in again.

Question: Is my code to set the maxDate correct.
Question: Is populating the textbox from the code behind the right way to do this.

Would it be better to set both defaultDate and maxDate to  "<%= DefaultToDate%>" and forget about setting the value in the code behind?

I want the default value to populate the textbox and show the datepicker set to that date with any date past that date blocked from selection.

Up till now if I do anything with the defaultDate or the maxDate the datepicker stops dropping down when I click on the field. If I only have
                 changeMonth: true,
                 changeYear: true,
                 dateFormat: "mm/dd/yyyy",
then the calendar drops down when I click in the To date: field

I'm thinking I need advice on parsing "<%= DefaultToDate%>"  in the maxDate parameter.



Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question