我在表单中有一个开始日期和结束日期字段。我已经指定了日历扩展器的格式。从那以后,比较验证器不起作用。它始终显示错误消息。请帮忙。我需要以“2012年5月4日星期五”的格式显示日期。
开始日期字段:
<asp:TextBox ID="txtStartDate" ReadOnly="true" runat="server"
CssClass="textBoxWidth TPRValue" Text='<%#DataBinder.Eval(Container.DataItem, "StartDate", "{0: ddd MM dd, yyyy}")%>'>
</asp:TextBox>
<asp:ImageButton ID="imgBtnStartDate" runat="server" ImageUrl="~/Common/Images/Calendar.GIF" CausesValidation="false" ImageAlign="AbsMiddle" />
<ajax:CalendarExtender ID="StartDateCalendar" TargetControlID="txtStartDate" PopupButtonID="imgBtnStartDate" runat="server"
Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>
<asp:CompareValidator ID="startDateCompareValidator" runat="server" ControlToValidate="txtStartDate" ControlToCompare="txtEndDate" Enabled="true"
Type="Date" Display="Dynamic" Operator="LessThanEqual"
Text="Startdate should be <= enddate">
</asp:CompareValidator>
EndDate字段:
<asp:TextBox ID="txtEndDate" ReadOnly="true" runat="server"
CssClass="textBoxWidth TPRValue" Text='<%#DataBinder.Eval(Container.DataItem, "EndDate", "{0: ddd MM dd, yyyy}")%>'>
</asp:TextBox>
<asp:ImageButton ID="imgBtnEndDate" runat="server" ImageUrl="~/Common/Images/Calendar.GIF" CausesValidation="false" ImageAlign="AbsMiddle" />
<ajax:CalendarExtender ID="EndDateCalendar" TargetControlID="txtEndDate" PopupButtonID="imgBtnEndDate" runat="server"
Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>
比较验证器:
<asp:CompareValidator ID="startDateCompareValidator" runat="server" ControlToValidate="txtStartDate"
ControlToCompare="txtEndDate" Enabled="true" Type="Date" Display="Dynamic" Operator="LessThanEqual"
Text="Startdate should be <= enddate">
</asp:CompareValidator>
答案 0 :(得分:4)
我认为CompareValidator
不接受您的格式。
CompareValidator非常特别关注它会接受的日期。例如,以下日期不被视为有效:
CompareValidator需要一个如下所示的日期:
http://www.informit.com/articles/article.aspx?p=25461&seqNum=5
未经测试,您可以尝试使用隐藏的TextBox(display:none
),并将接受的日期格式作为文本。然后将Validator的ControlToValidate
设置为“hiddenfield”。您需要将TextBoxes的Text属性与其隐藏字段同步。也许这会给你一个想法。
编辑:好的,我已经尝试让它按照我说的工作了,实际上它正在工作:) 也许有一些重构可能,但看看你自己。
要使用工作日期格式隐藏TextBox,我使用了CSS:
<style type="text/css">
.hidden
{
display:none;
}
</style>
当用户通过CalendarExtenders更改日期时,将调用这些JS函数:
<script type="text/javascript">
function dateChangedStart(sender, args) {
var selectedDate = sender.get_selectedDate();
var hiddenStart = $get("txtStartDateHidden");
var validator = $get("startDateCompareValidator");
hiddenStart.value = dateToString(selectedDate);
ValidatorValidate(validator);
}
function dateChangedEnd(sender, args) {
var selectedDate = sender.get_selectedDate();
var hiddenEnd = $get("txtEndDateHidden");
var validator = $get("startDateCompareValidator");
hiddenEnd.value = dateToString(selectedDate);
ValidatorValidate(validator);
}
function dateToString(d) {
var year = d.getFullYear();
var month = d.getMonth() + 1; //months are zero based
var day = d.getDate();
return year + "/" + month + "/" + day;
}
</script>
这是示例页面的其余部分:
<div>
<asp:TextBox ID="txtStartDate" CausesValidation="false" ReadOnly="true" runat="server">
</asp:TextBox>
<asp:TextBox ID="txtStartDateHidden" CssClass="hidden" ValidationGroup="DateCheck" CausesValidation="true" ReadOnly="false" runat="server">
</asp:TextBox>
<ajax:CalendarExtender ID="StartDateCalendar" TargetControlID="txtStartDate" runat="server"
OnClientDateSelectionChanged="dateChangedStart"
Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>
<asp:CompareValidator ID="startDateCompareValidator" runat="server" EnableClientScript="true"
ControlToValidate="txtStartDateHidden" Display="Static" Operator="LessThanEqual" ValidationGroup="DateCheck"
ControlToCompare="txtEndDateHidden" Enabled="true" Type="Date" Text="Startdate should be <= enddate">
</asp:CompareValidator>
<asp:TextBox ID="TxtEndDate" CausesValidation="false" ReadOnly="true" runat="server">
</asp:TextBox>
<asp:TextBox ID="txtEndDateHidden" CssClass="hidden" ValidationGroup="DateCheck" CausesValidation="true" ReadOnly="false" runat="server">
</asp:TextBox>
<ajax:CalendarExtender ID="EndDateCalendar" TargetControlID="txtEndDate" runat="server"
OnClientDateSelectionChanged="dateChangedEnd"
Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>
<asp:Button ID="BtnSubmit" CausesValidation="true" ValidationGroup="DateCheck" runat="server" Text="Submit" />
</div>
答案 1 :(得分:0)
除了Tim的答案之外,还有那些想要使用jQuery的人:
function dateChangedStart(sender, args) {
var selectedDate = sender.get_selectedDate();
var hiddenStart = $('input[id$=txtStartDateHidden]');
var validator = $('span[id$=startDateCompareValidator]');
hiddenStart.val(dateToString(selectedDate));
var validatorAsDOM = validator.get(0);
ValidatorValidate(validatorAsDOM);
}
答案 2 :(得分:0)
日期格式MM / dd / yyyy
把它放在Web.Config
中<globalization culture="en-us"/>
下的
<system.web>