c#.net使用asp:CompareValidator和日期 [英] c#.net using asp:CompareValidator with dates

查看:123
本文介绍了c#.net使用asp:CompareValidator和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript增强文本框生成的日期格式为dd / mm / yyyy,当使用asp:comparevalidator验证时无法正确验证。



以下是相关代码:

 < asp:CompareValidator ID =CompareValidator4runat =server
ControlToValidate =txtEndDateValueToCompare =txtStartDate
显示=无
ErrorMessage =结束日期应大于或等于开始日期。
Type =DateOperator =GreaterThanEqualSetFocusOnError =True>
< / asp:CompareValidator>

< strong>开始日期< / strong>< asp:TextBox ID =txtStartDaterunat =serverWidth =215pxCssClass =textfieldStyle =width:176px; margin-left:5px;>< / asp:TextBox>& nbsp;& nbsp;

< strong>结束日期< / strong>< asp:TextBox ID =txtEndDaterunat =serverWidth =215pxCssClass =textfieldStyle =width:176px; margin-left:5px;>< / asp:TextBox>& nbsp;& nbsp;

如果不清楚,我希望txtStartDate中的日期早于 txtEndDate



当验证失败时,我会获得乱码的结果,没有明显的模式。谁能看到有什么问题?



btw我知道html有多差 - 我正在编辑别人的代码。

解决方案

您可以将页面的文化更改为 en-GB 吗?

 <%@页面语言=C#AutoEventWireup =trueCodeFile =Default.aspx.cs
Inherits =_ DefaultCulture =en-GB%>

找到 here



更新您的验证器标记中有错误。如果要比较两个文本框日期,您必须提供 ControlToValidate ControlToCompare (而不是 ValueToCompare

 < asp:CompareValidator ID =CompareValidator4 =server
ControlToValidate =txtEndDate
ControlToCompare =txtStartDate
Type =DateOperator =GreaterThanEqual
显示=无ErrorMessage =结束日期应大于等于开始日期。 SetFocusOnError =True>
< / asp:CompareValidator>


I have dates generated by a Javascript enhanced textbox in the format dd/mm/yyyy, which when validated with a asp:comparevalidator fails to to validate correctly.

Here is the relevant code:

<asp:CompareValidator ID="CompareValidator4" runat="server" 
                      ControlToValidate="txtEndDate" ValueToCompare="txtStartDate" 
                      Display="None" 
                      ErrorMessage="End Date should be greater than or equal to Start Date." 
                      Type="Date" Operator="GreaterThanEqual" SetFocusOnError="True">    
</asp:CompareValidator>

<strong>Start Date</strong><asp:TextBox ID="txtStartDate" runat="server" Width="215px" CssClass="textfield" Style="width: 176px; margin-left:5px;"></asp:TextBox>&nbsp;&nbsp;

<strong>End Date</strong><asp:TextBox ID="txtEndDate" runat="server" Width="215px" CssClass="textfield" Style="width: 176px;  margin-left:5px;"></asp:TextBox>&nbsp;&nbsp;

In case it isn't clear I want the date in txtStartDate to be earlier than the one in txtEndDate.

When validated, I get gibberish results with no obvious pattern to when the validation fails. Can anyone see what is wrong?

btw I am aware of how poor the html is - I am editing someone else's code.

解决方案

Can you change the page's culture to en-GB?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" Culture = "en-GB" %>

Found here.

Update You have a bug in your validator markup. If you want to compare both textbox-dates with each other you have to provide the ControlToValidate and the ControlToCompare (not the ValueToCompare)

 <asp:CompareValidator ID="CompareValidator4" runat="server" 
    ControlToValidate="txtEndDate" 
    ControlToCompare="txtStartDate" 
    Type="Date" Operator="GreaterThanEqual"
    Display="None" ErrorMessage="End Date should be greater than or equal to Start Date." SetFocusOnError="True">
</asp:CompareValidator>

这篇关于c#.net使用asp:CompareValidator和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆