Search code examples
javascriptc#asp.netdateascx

Clearing date using c#


i am having a problem clearing a date text box. When ever i do a post back the date returns. I have tried multiple methods to clear the date the main one is calling into the in house library for the date control adding a clear button that clears it server side. I have also tried using the same clear button to clear it client side using JavaScript. No matter what method i use the original date returns to the box. This is causing validation errors when saving the page.

This is the original boxes with the dates in

Date text boxes

If i clear the start date it disappears

start cleared

When i clear the end date the start date returns

End cleared

This is how i clear the dates

protected void ClearDate(object sender, EventArgs e)
{
    this.Value = string.Empty;
}

which when clicked goes into a get/set for Value and runs the following code

            if (value == null || value == string.Empty)
            {
                _dateTextBox.Text = "";
                if (ShowSeconds)
                    _timeTextBox.Text = "00:00:00";
                else
                    _timeTextBox.Text = "00:00";
                _altTextBox.Text = "";
                _calExtender.SelectedDate = null;
            }

The issue is that when i change the value of one date box then clear the other the value returns in the changed one to the previous value, it appears to be related to the calendar extender. It seems no matter how many times i go through it the calendar extender holds the original value.

I also tried to use JavaScript to do it however, the ID isn't fully generated server side, so i can only get a portion of it and for some reason if i used a guid and set the id generation to static when ever the page does a post back it changes the guid and this seems to revert the values in the date text boxes back to their original values.

function clearDateTextBox(dateTextBoxId) {
    document.getElementById(dateTextBoxId).value = "";
}

Does anyone have any suggestions of ways to go about clearing the date from the text box that would persist a post back. I have tried both enabling view state and disabling view state on the text box.


Solution

  • As the text box, calendar extender and tool tips are bundles up within a single control when setting the value within the calendar extender's selected value it would return it on post back, as this is not required as the textbox will hold the current value it was easier to clear the selected value from the calendar extender or better just omit setting it at all during the page build action. I removed it completely and solved the problem of the original value been retained.