ASP.NET AJAX CalendarExtender不会更新SelectedDate值NET、ASP、AJAX、SelectedDate

2023-09-10 17:14:18 作者:点到为止

由于某些原因,上正在开发中的ASP.NET网站的任何CalendarExtenders将不会被更新。我已经检查了所有的明显的地方(如的AutoPostBack和AutoEventHandler)。问题是,当我从日历中选择一个日期,并将其发布到形式,正在扩展文本框被更新,但日历扩展的日期根本就没有被更新(例如SelectedDate还是和以前一样) 。我用Google搜索任何可能的解决方案,但都没有奏效。

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.

下面是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" />

和codebehind:

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        
        }
    }
}

在我的code到达做回发的行为,txtBlahExtender.SelectedDate总是DateTime.Today.AddDays(4)。它根本没有办理变更登记。

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

任何想法?

谢谢, Logain史密斯

Thanks, Logain Smith

(是否有可能做一个格式化的问题?)

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

推荐答案

在网上搜索了无数次后,似乎没有为这个问题的修正。一个解决方案(如果你想叫的话)可以使用转换从文本框手动分配SelectedDate(这需要您设置格式的标记,虽然):

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;
}

(凡嗒嗒是文本控件和blahCalendarExtender是扩展延伸等等)

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

看来,calendarExtender控制应该是足够聪明的,虽然这样做对自己。

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