如何在没有部分更改事件的情况下确认预先选择的日期?

发布于 2024-12-09 07:49:40 字数 804 浏览 0 评论 0原文

我的是 C# Web 应用程序。 我有一个日历控件,默认将今天作为所选日期。 问题是,如果用户单击所选日期,那么由于没有 更改日期时 SelectedChanged 不会触发。没有其他控制 触发的事件。我如何确认用户选择 预先选定的日期?

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {               
            if (ddlCityNames.SelectedIndex == 0 || ddlHotelNames.SelectedIndex == 0)
            {
                if(ddlCityNames.SelectedIndex==0)
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Select City');", true);   
                else
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Select Hotel');", true);
            }
           }

当用户更改他的选择时,会调用此方法。但是,如果他选择了一个日期并调用了此验证,那么他之前所做的选择仍然存在,但单击该选择不会执行任何操作。当我选择另一个日期并来时在该日期再次可行,但之前选择的日期又如何呢?

Mine is c# web application.
I have a calendar control that defaults to today as the selected date.
Problem is, if the user clicks on the selected date then since there is no
change in date the SelectedChanged does not fire. There is no other control
event that fires either. How do I acknowledge the user selection of a
pre-selected date?

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {               
            if (ddlCityNames.SelectedIndex == 0 || ddlHotelNames.SelectedIndex == 0)
            {
                if(ddlCityNames.SelectedIndex==0)
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Select City');", true);   
                else
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Select Hotel');", true);
            }
           }

This is called when user changes his selection.But in case if he selects a date and this validations are called,after that previous selection that he has made is stil there but on click on that selection does nothing.when I select another date and come again on that date is works but what about previously selected date?

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

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

发布评论

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

评论(1

如痴如狂 2024-12-16 07:49:40

查看您的代码,将服务器端验证逻辑移植到客户端会更好。本质上,您必须连接到支持输入(日历控件)的 blur 事件并验证您的下拉菜单。这也将消除日期选择的回发。

在服务器端,验证应该在将使用所有数据的地方完成 - 例如,如果您在填写酒店、城市和日期后调用搜索,则应在 Search 按钮上进行验证。

编辑

这里有一些示例代码(基于 jquery),可以帮助您入门。我假设您使用的是 AJAX Toolkit Calender Extender 或一些基于 java 脚本的 UI,例如 jquery date-picker 顺便说一下,这将是我的偏好)。通过这两种方法,您可以完全控制支持输入字段。

因此,使用 AJAX 工具包,标记将类似于

<asp:TextBox runat="server" ID="Date1" />
<ajaxToolkit:Calendar runat="server" TargetControlID="Date1" />

现在您可以在标记 (aspx) 文件中包含以下脚本:

$(document).ready(function() {
   var date = $('#<%= Date1.ClientID %>');
   var city = $('#<%= ddlCityNames.ClientID %>');
   var hotel = $('#<%= ddlHotelNames.ClientID %>');

   date.blur(function() {
     // check if date has been selected
     if (date.val() != '') {
       if (city.val() == '') { // **** put value at index zero that you use for no-selection
          alert('Select City');
       }
       else if (hotel.val() == '') {
          alert('Select Hotel');
       }
     }
   });
});

Looking at your code, you will be better off by porting you server side validation logic at the client side. Essentially, you have to hook up into blur event of backing input (of calender control) and validate your drop-downs. This will also eliminate the post-back on the date selection.

On server side, the validation should be done at the place where all the data will be used - for example, Search button if you are invoking search after hotel, city and date are filled.

EDIT:

Here's some sample code (based on jquery) that should get you started. Instead of Calender control (whose markup I am not familiar with), I am assuming that you are using AJAX Toolkit Calender Extender or some java-script based UI such as jquery date-picker which will be my preference by the way). With both of these approaches, you have full control over backing input field.

So with AJAX toolkit, markup will be something like

<asp:TextBox runat="server" ID="Date1" />
<ajaxToolkit:Calendar runat="server" TargetControlID="Date1" />

Now you can have following script in your markup (aspx) file:

$(document).ready(function() {
   var date = $('#<%= Date1.ClientID %>');
   var city = $('#<%= ddlCityNames.ClientID %>');
   var hotel = $('#<%= ddlHotelNames.ClientID %>');

   date.blur(function() {
     // check if date has been selected
     if (date.val() != '') {
       if (city.val() == '') { // **** put value at index zero that you use for no-selection
          alert('Select City');
       }
       else if (hotel.val() == '') {
          alert('Select Hotel');
       }
     }
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文