CalendarExtender 使用 Javascript 更改日期

发布于 2024-07-19 03:11:51 字数 280 浏览 8 评论 0原文

我的页面上有一个 CalendarExtender 控件,有时必须将日期更改为下一个周日。 我当前正在使用控件的 OnClientDateSelectionChanged 属性来调用一个函数,该函数将在日期中添加一些天数,直到周日。

我遇到的问题是,如果我在日历中选择星期二,文本框将显示下一个星期日,但日历中选定的日期仍然是星期二。

如何更新 CalendarExtender 以获取我在 javascript 中选择的新日期? CalendarExtendar 连接的文本框显示正确的日期...

I have a CalendarExtender control on my page and sometimes have to change the date to the next occuring Sunday. I'm currently using the OnClientDateSelectionChanged property of the control to call a function that will add some days to the date until its Sunday.

The problem I'm having is that if I select a Tuesday in my calendar, the textbox will display the next Sunday but the selected date in the calendar is still Tuesday.

How do I update the CalendarExtender to have the new date that has one I selected in javascript? The textbox the CalendarExtendar is connected to shows the correct date...

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

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

发布评论

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

评论(2

悍妇囚夫 2024-07-26 03:11:51

,则更改 CalendarExtenderTargetControlId 文本框的值会影响所选日期

  1. 如果满足以下 2 个条件 onchange 事件在文本框中触发(通过手动更改文本或调用显式 javascript fireEvent() 方法。
  2. 在文本框中输入的日期的格式与使用的相同格式匹配 也就是说,处理此问题的正确方法是调用CalendarExtender

控件的 set_selectedDate() 函数,这一调用不仅会设置 Calendar 上的选定内容。同时在目标文本框中

显示以下示例代码:

<cc1:CalendarExtender ID="CalendarExtender1" runat="server" 
        OnClientDateSelectionChanged="dateSelectionChanged" 
        TargetControlID="txtDate" PopupButtonID="imgCalendar">
</cc1:CalendarExtender>

<script type="text/javascript">
  function dateSelectionChanged(sender, args){
    selectedDate = sender.get_selectedDate();
    /* replace this next line with your JS code to get the Sunday date */
    sundayDate = getSundayDateUsingYourAlgorithm(selectedDate); 
    /* this sets the date on both the calendar and textbox */
    sender.set_SelectedDate(sundayDate); 
 }
</script>

Changing the value of the textbox that is the TargetControlId for the CalendarExtender affects the selected date if the following 2 conditions are met:

  1. An onchange event is fired on the textbox (either by changing the text manually or by calling an explicit javascript fireEvent() method.
  2. The format of the date entered in the textbox matches the same format used by the CalendarExtender control.

That being said, the correct way to handle this is to call the set_selectedDate() function of the CalendarExtender control. This one call, not only sets the selected on the Calendar, but also on the Targeted textbox at the same time.

Here's the example code:

<cc1:CalendarExtender ID="CalendarExtender1" runat="server" 
        OnClientDateSelectionChanged="dateSelectionChanged" 
        TargetControlID="txtDate" PopupButtonID="imgCalendar">
</cc1:CalendarExtender>

<script type="text/javascript">
  function dateSelectionChanged(sender, args){
    selectedDate = sender.get_selectedDate();
    /* replace this next line with your JS code to get the Sunday date */
    sundayDate = getSundayDateUsingYourAlgorithm(selectedDate); 
    /* this sets the date on both the calendar and textbox */
    sender.set_SelectedDate(sundayDate); 
 }
</script>
℡寂寞咖啡 2024-07-26 03:11:51
<asp:TextBox ID="txtDate" Text='<%# Bind("Date", "{0:dd-MMM-yyyy}") %>'
                                                                                            runat="server" class="form-control input-sm m-bot15" BackColor="#ffccbb"></asp:TextBox>
                                                                                        <asp:CalendarExtender ID="CalExtender1" runat="server" Enabled="true" Format="dd-MMM-yyyy"
                                                                                            TargetControlID="txtDate">
                                                                                        </asp:CalendarExtender>
<asp:TextBox ID="txtDate" Text='<%# Bind("Date", "{0:dd-MMM-yyyy}") %>'
                                                                                            runat="server" class="form-control input-sm m-bot15" BackColor="#ffccbb"></asp:TextBox>
                                                                                        <asp:CalendarExtender ID="CalExtender1" runat="server" Enabled="true" Format="dd-MMM-yyyy"
                                                                                            TargetControlID="txtDate">
                                                                                        </asp:CalendarExtender>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文