ASP.NET Ajax CalendarExtender will not update SelectedDate value

前提是你 提交于 2020-01-02 01:41:14

问题


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:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3621112/asp-net-ajax-calendarextender-will-not-update-selecteddate-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!