如何在没有部分更改事件的情况下确认预先选择的日期?
我的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看您的代码,将服务器端验证逻辑移植到客户端会更好。本质上,您必须连接到支持输入(日历控件)的
blur
事件并验证您的下拉菜单。这也将消除日期选择的回发。在服务器端,验证应该在将使用所有数据的地方完成 - 例如,如果您在填写酒店、城市和日期后调用搜索,则应在
Search
按钮上进行验证。编辑:
这里有一些示例代码(基于 jquery),可以帮助您入门。我假设您使用的是 AJAX Toolkit Calender Extender 或一些基于 java 脚本的 UI,例如 jquery date-picker 顺便说一下,这将是我的偏好)。通过这两种方法,您可以完全控制支持输入字段。
因此,使用 AJAX 工具包,标记将类似于
现在您可以在标记 (aspx) 文件中包含以下脚本:
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
Now you can have following script in your markup (aspx) file: