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

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.

Bob
LVL 1
bob_mechlerProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
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-?
0
Alexandre SimõesManager / Technology SpecialistCommented:
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:
http://api.jqueryui.com/datepicker/#method-getDate
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bob_mechlerProgrammerAuthor Commented:
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
        {
            get;
            set;
        }

 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.

Thanks,

Bob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.