Asp.Net结束日期大于开始日期

时间:2010-02-13 10:10:44

标签: asp.net

Asp.Net与08的合作。 以下是我的代码

<asp:TextBox ID="txtSTART_DATE" runat="server"></asp:TextBox>
     <cc1:PopupControlExtender ID="txtSTART_DATE_PopupControlExtender" runat="server" CommitProperty="value"
            CommitScript="e.value += '';" PopupControlID="pnlSTART_DATE"
            Position="Bottom" TargetControlID="txtSTART_DATE">
           </cc1:PopupControlExtender> 

           <asp:TextBox ID="txtEND_DATE" runat="server" 
        ontextchanged="txtEND_DATE_TextChanged"></asp:TextBox>
     <cc1:PopupControlExtender ID="txtEND_DATE_PopupControlExtender" runat="server" CommitProperty="value"
            CommitScript="e.value += '';" PopupControlID="pnlEND_DATE"
            Position="Bottom" TargetControlID="txtEND_DATE">
           </cc1:PopupControlExtender> 

C#语法是

protected void dtpSTART_DATE_SelectionChanged(object sender, EventArgs e)
{
  AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpSTART_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}

protected void dtpEND_DATE_SelectionChanged(object sender, EventArgs e)
{
  AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpEND_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}

想要比较开始日期和结束日期。验证结束日期大于开始日期和今天日期。如何检查值?

2 个答案:

答案 0 :(得分:2)

您可以使用asp.net CompareValidator,例如

<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="cmpDates" ControlToValidate="txtEND_DATE"
    ControlToCompare="txtSTART_DATE" Operator="GreaterThan" Display="dynamic" 
    ErrorMessage="End date must be after start date" runat="server" />

虽然这不会弹出一个弹出窗口,但是如果你只是在验证器之后那么那应该没问题。

比较验证器实际上非常强大,例如,你也可以检查参数的数据类型,所以我会使用这样的东西:

<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:CompareValidator ID="chkStartIsDate" runat="server" Display="Dynamic"
    Operator="DataTypeCheck" Type="Date" ControlToValidate="txtSTART_DATE"
    ErrorMessage="You must supply a valid start date" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="chkEndIsDate" runat="server" Display="Dynamic"
    Operator="DataTypeCheck" Type="Date" ControlToValidate="txtEND_DATE"
    ErrorMessage="You must supply a valid end date" />
<asp:CompareValidator ID="cmpStartAndEndDates" runat="server" Display="Dynamic"
    Operator="GreaterThan" ControlToValidate="txtEND_DATE" ControlToCompare="txtSTART_DATE"
    ErrorMessage="The end date must be after the start date" />

答案 1 :(得分:0)

<asp:TextBox ID="txtStartDate" runat="server" />
<asp:TextBox ID="txtEndDate" runat="server" />
<asp:CompareValidator ID="cvStartEnd" Operator="GreaterThan" Type="Date"
    ControlToValidate="txtEndDate" ControlToCompare="txtStartDate"
    ErrorMessage="End date must be greater than start date!" runat="server"/>

确保设置属性Type="Date"否则会进行字符串比较并给出错误的结果。