0

我想比较开始日期和结束日期,即。开始日期应小于结束日期。我正在动态更改页面文化。但是,比较验证器仅比较 dd/mm/yyyy 格式的日期。

这是我使用的覆盖方法:

 protected override void InitializeCulture()
{
    string datetimeculture = "en-US"; // default lang
    if (Session["datetimeculture"] != null)
    {
        datetimeculture = Session["datetimeculture"].ToString();
    }

    this.UICulture = datetimeculture;
    this.Culture = datetimeculture;
    base.InitializeCulture();
}

这是比较验证器

<asp:CompareValidator ID="DateCompare" runat="server" ControlToCompare="txtfaredate_to" 
ControlToValidate="txtfaredate_from" ForeColor="Red" Type="Date" Operator="LessThanEqual" display="Dynamic"
CssClass="failureNotification" ErrorMessage="The Fare Date form must not match the Fare Date to."
ValidationGroup="basic">To Date cannot be less than From Date.</asp:CompareValidator>

任何想法如何做到这一点?

谢谢,

4

2 回答 2

0

我使用自定义验证器来解决我的问题。

<asp:CustomValidator ID="DateCompare" runat="server" ControlToValidate="txtfaredate_to" EnableClientScript="true" ForeColor="Red" display="Dynamic"
 ErrorMessage="To Date cannot be less than From Date." ClientValidationFunction="fncClientCheckDate"></asp:CustomValidator>

客户端代码:

 function fncClientCheckDate(sender, args) {
        var todate = dateToYMD(document.getElementById("txtfaredate_to").value);
        var fromdate = dateToYMD(document.getElementById("txtfaredate_from").value);

        if (new Date(todate) < new Date(fromdate))
    {
    args.IsValid = false;
    return;
    }
    args.IsValid = true;
    }
    function dateToYMD(date) {
        var date = new Date(date);
       // alert(date);
        var d = date.getDate();
        var m = date.getMonth() + 1;
        var y = date.getFullYear();
        return '' + y + '-' + (m <= 9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
    }
于 2012-11-28T08:15:54.027 回答
0

尝试设置 Validator 控件的 ValueToCompare 属性。像这样的东西:

<asp:CompareValidator ID="DateCompare" runat="server" 
   ControlToCompare="txtfaredate_to" 
   ControlToValidate="txtfaredate_from"
   ValueToCompare="<%# DateTime.Now.ToString(\"dd-MM-yyyy\") %>" //adjust to your needs 
   ForeColor="Red" 
   Type="Date" 
   Operator="LessThanEqual" 
   display="Dynamic"
   CssClass="failureNotification" 
   ErrorMessage="The Fare Date form must not match the Fare Date to."
   ValidationGroup="basic">To Date cannot be less than From Date.
</asp:CompareValidator>
于 2012-11-28T06:44:18.050 回答