Link to home
Start Free TrialLog in
Avatar of bob_mechler
bob_mechlerFlag for United States of America

asked on

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
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

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-?
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bob_mechler

ASKER

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