asp.nettextboxpostbackcalendarextender

Textbox not retaining its value after postback using a calendar extender


I'm using CalendarExtender with a textbox as the target control. My code is below:

<asp:TextBox ID="textBoxFromDate" runat="server" EnableViewState="true" />
<asp:CalendarExtender ID="textBoxFromDate_CalendarExtender" runat="server" Enabled="True"
                    TargetControlID="textBoxFromDate" Format="dd/MM/yyyy" EnableViewState="true">
</asp:CalendarExtender>

So I use the calendar control to set the value within the textbox. This works fine, however when a postback happens on the page the textbox value is reset to the default date for the calendar control.

How can I get the textbox to retain its value on a postback?

Thanks in advance.

I'm using ASP.Net 4.0.


Solution

  • did you try setting readonly field dynamically rather than in markup ? Also i'd make sure you are not resetting the value in Page_Load() during postbacks.

    Or what about trying somehting like this on Page_load , also checking to see if there is a valid date in box before attempting to set it to the calendar extender selected date ?

    if (IsPostBack) 
    { 
        MyCalendarExtender.SelectedDate =
            DateTime.ParseExact(MyTextBox.Text, MyCalendarExtender.Format, null); 
    }