12

For some reason, any CalendarExtenders on an ASP.NET site that is being worked on will not be updated. I have already checked all the obvious places (such as AutoPostBack and AutoEventHandler). The problem is that when I select a date from the Calendar and post it to the form, the TextBox that is being extended IS being updated, but the calendar extender's date is simply not being being updated (e.g. SelectedDate is still the same as before). I have googled for any possible solutions but none have worked.

Here is the code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master"
    AutoEventWireup="true" CodeBehind="ThePage.aspx.cs" Inherits="ThePage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:TextBox runat="server" ID="txtBlah" />
<asp:CalendarExtender ID="txtBlahExtender" runat="server" TargetControlID="txtBlah" Format="MMMM d, yyyy" />
<asp:Button runat="server" ID="btnSubmit" CausesValidation="false" />

and the codebehind:

public partial class ThePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtBlahExtender.SelectedDate = DateTime.Today.AddDays(4);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
         //do postback actions        
        }
    }
}

When my code reaches "do postback actions", txtBlahExtender.SelectedDate is ALWAYS DateTime.Today.AddDays(4). It simply doesn't register the change.

Any ideas?

Thanks, Logain Smith

(Is it possible to do formatting on a question?)

1
  • I'm running into the same problem Sep 22, 2010 at 19:19

3 Answers 3

15

After searching the Internet countless times, there doesn't appear to be a fix for this problem. A solution (if you want to call it that) could be to manually assign SelectedDate using conversion from the textbox (this requires you to set the format in the markup, though):

if(IsPostBack) {
blahCalendarExtender.SelectedDate = DateTime.ParseExact(blah.Text, blahCalendarExtender.Format, null);
// do postback actions
} else {
// for instance, maybe initalize blahCalendarExtender to today
blahCalendarExtender.SelectedDate = DateTime.Today;
}

(Where blah is the Text Control and blahCalendarExtender is the extender extending blah)

It seems that the calendarExtender control should be intelligent enough to do this on its own though.

1
  • 1
    I set blahCalendarExtender.SelectedDate = null; So I avoid problems if user inserted some not parseble date in blah.Text. The last choosen date by user is filled automatically by the view state.
    – Ephedra
    Jul 17, 2014 at 9:47
0

Make sure to put the texbox and extender in an UpdatePanel (I don't see this in the code you provided).

2
  • Update - didn't work. After adding the UpdatePanel the value immediately reverts so the initial value and refuses to left me change.
    – logain
    Sep 1, 2010 at 19:41
  • Mine is already in an updatePanel and it makes no difference :(
    – Zeek2
    Feb 24, 2020 at 15:31
0

I have found a very strange solution for this.

Do not initialize the value for the textbox which has calendar extender attached. Keep the textbox blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.