ASP.NET Ajax CalendarExtender 将不会更新 SelectedDate 值

发布于 2024-09-16 13:33:54 字数 1348 浏览 3 评论 0原文

由于某种原因,正在处理的 ASP.NET 站点上的任何 CalendarExtender 将不会更新。我已经检查了所有明显的地方(例如 AutoPostBack 和 AutoEventHandler)。问题是,当我从日历中选择一个日期并将其发布到表单时,正在扩展的文本框正在更新,但日历扩展器的日期根本没有被更新(例如 SelectedDate 仍然与以前相同) 。我用谷歌搜索了任何可能的解决方案,但没有一个有效。

这是代码:

<%@ 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" />

和代码隐藏:

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

当我的代码达到“执行回发操作”时,txtBlahExtender.SelectedDate 始终为 DateTime.Today.AddDays(4)。它根本不记录更改。

有什么想法吗?

谢谢, Logain Smith

(是否可以对问题进行格式化?)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-09-23 13:33:54

在网上查了无数遍,似乎没有解决这个问题的办法。一个解决方案(如果您想这样称呼它)可能是使用文本框的转换来手动分配 SelectedDate (但这需要您在标记中设置格式):(

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

其中 blah 是文本控件,blahCalendarExtender 是扩展器等等)

不过,calendarExtender 控件似乎应该足够智能,可以自行完成此操作。

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.

小情绪 2024-09-23 13:33:54

确保将文本框和扩展器放入 UpdatePanel 中(我在您提供的代码中没有看到这一点)。

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

万劫不复 2024-09-23 13:33:54

我为此找到了一个非常奇怪的解决方案。

不要初始化附加了日历扩展器的文本框的值。将文本框保留为空。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文