How to synchronize calendar date pickers with date fields

An issue can arise when a user inputs a date directly in the date field and then later wants to edit that date using the calendar date picker, where the date selected in the calendar isn’t updated, as shown here:

To resolve this issue, you have to add a function in the date field, on the onTextChanged event:


Then, add the following code in the code-behind:

protected void DATE_TextChanged(object sender, EventArgs e){
        DateTime date;
        if (DateTime.TryParse(MODIFIE_DATE.Text, out date))
            MODIFIE_DATE_CAL.SelectedValue = (DateTime?)date;

This will update the selected value of the calendar with the latest input value, and will be triggered when the text in the date field is changed.

However, this won’t update the calendar selected value if the date inserted in text format is invalid, as shown here: