ASP.NET Ajax CalendarExtender will not update Sele

2019-04-22 19:07发布

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?)

3条回答
放荡不羁爱自由
2楼-- · 2019-04-22 19:49

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-22 19:54

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.

查看更多
神经病院院长
4楼-- · 2019-04-22 20:05

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

查看更多
登录 后发表回答