简体   繁体   English

ASP.NET 日历未更新

[英]ASP.NET Calendar Not Updating

I'm adding a Calendar to to my application and am having a hard time updating the date.我正在向我的应用程序添加一个日历,并且很难更新日期。 For some reason when I select a new date the previous date is displayed.由于某种原因,当我 select 显示前一个日期的新日期时。 So if it starts on the 11th and I select the 13th, the 11th is displayed again when the page reloads and then if I select the 14th after that the 13th will load.因此,如果它在 11 日开始,而我在 13 日开始 select,则在页面重新加载时再次显示第 11 日,然后如果我在第 14 日 select 之后,第 13 日将加载。

I declare the calendar in the application like this:我在应用程序中声明日历,如下所示:

    <asp:Calendar ID="myCal" runat="server"></asp:Calendar>

And then I have this code in page_init:然后我在 page_init 中有这段代码:

    myCal.SelectedDate = DateTime.Today.AddDays(1);

And use this in page_load:并在 page_load 中使用它:

    String date = myCal.SelectedDate.ToString("yyyyMMdd");

Thanks.谢谢。

Your calendar probably triggers a postback, but the Page_Load event occurs before any control events.您的日历可能会触发回发,但Page_Load事件发生在任何控制事件之前。 That means you are loading the "previous" selected value each time in the Page_Load method.这意味着您每次都在Page_Load方法中加载“先前”选定的值。

You should move your string date = myCal.SelectedDate.ToString("yyyyMMdd");你应该移动你的string date = myCal.SelectedDate.ToString("yyyyMMdd"); and its associated usage from the Page_Load to the SelectionChanged event handler for the calendar control.及其从Page_Load到日历控件的SelectionChanged事件处理程序的相关用法。

So create the following method:所以创建以下方法:

protected void myCal_SelectionChanged(Object sender, EventArgs e)
{
    string date = myCal.SelectedDate.ToString("yyyyMMdd");
    // lblMyLabel.Text = date;
    // Put your code that handles the selected date here.
}

And associate it with your calendar:并将其与您的日历相关联:

<asp:Calendar ID="myCal" runat="server"
    OnSelectionChanged="myCal_SelectionChanged"></asp:Calendar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM