0

我为用户创建文本框以选择日期并设置 requiredfieldvalidator。

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ControlToValidate="StartDate" ErrorMessage="RequiredFieldValidator" 
    ValidationGroup="Search" SetFocusOnError="True" Display="Dynamic" 
    ToolTip="*Fill START Date!">*Fill START Date!</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
    ControlToValidate="EndDate" ErrorMessage="RequiredFieldValidator" 
    ValidationGroup="Search" Display="Dynamic" ToolTip="*Fill END Date!">*Fill END  Date!</asp:RequiredFieldValidator>

<asp:Label ID="DateFilter" runat="server" 
    Text="Filter by Date from :" style="font-family: 'Arial Narrow'" 
    AssociatedControlID="StartDate"></asp:Label>
<asp:TextBox ID="StartDate" runat="server" ></asp:TextBox>
<a href="javascript:;" onclick="window.open('CalendarPopup.aspx?textbox=StartDate','cal','width=220,height=205,left=270,top=180')"><img id="Img1" 
    src="calendar-icon.gif"  border="0" runat="server" alt="calendar" 
    style="height: 23px" align="bottom"></a>

 <asp:Label ID="Label4" runat="server" Text="to" Font-Names="Arial Narrow" 
     AssociatedControlID="EndDate"></asp:Label>
 <asp:TextBox ID="EndDate" runat="server"></asp:TextBox>
 <a href="javascript:;" onclick="window.open('CalendarPopup.aspx?textbox=EndDate','cal','width=220,height=205,left=270,top=180')"><img id="Img2" 
    src="calendar-icon.gif" border="0" runat="server" alt="calendar" 
    style="height: 23px" dir="ltr"></a>&nbsp;
<asp:Button 
    ID="Search" runat="server" BackColor="Black" Font-Names="Arial Narrow" 
    ForeColor="White" Text="SEARCH" Width="73px" onclick="Search_Click" />

然后,我需要使用比较验证器比较第一个日期必须小于第二个日期。

<asp:CompareValidator ID="CompareValidator1" runat="server" 
    ControlToCompare="EndDate" ControlToValidate="StartDate" 
    ErrorMessage="CompareValidator" Operator="GreaterThanEqual" Type="Date" 
    ValidationGroup="Search" Display="Dynamic" SetFocusOnError="True">*First date must be less than or equal to Second 
date!</asp:CompareValidator>

两个验证器都不起作用。任何人请帮忙。谢谢..:) 西蒂

4

1 回答 1

2

更新,我以前没有看到它,但您需要让按钮与您的验证器属于同一验证组。试试这个:

<asp:Button ValidationGroup="Search"
    ID="Search" runat="server" BackColor="Black" Font-Names="Arial Narrow" 
    ForeColor="White" Text="SEARCH" Width="73px" onclick="Search_Click" />

哦,我很确定您需要我之前提到的其他逻辑运算符,因为 ControlToValidate 是根据 ControlToCompare 评估的主要值,因此 ControlToValidate 是 LessThan ControlToCompare = StartDate 是 LessThan EndDate:

<asp:CompareValidator ID="CompareValidator1" runat="server"
    ControlToCompare="EndDate" ControlToValidate="StartDate" 
    ErrorMessage="CompareValidator" Operator="LessThan" Type="Date" 
    ValidationGroup="Search" Display="Dynamic" SetFocusOnError="True">*First date must be less than or equal to Second date!</asp:CompareValidator>

由于您遇到问题,我已经完整地检查了代码,并且它完全可以正常工作。复制并粘贴这个:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
ControlToValidate="StartDate" ErrorMessage="RequiredFieldValidator" 
ValidationGroup="Search" SetFocusOnError="True" Display="Dynamic" 
ToolTip="*Fill START Date!">*Fill START Date!</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
ControlToValidate="EndDate" ErrorMessage="*Fill END Date!" 
ValidationGroup="Search" Display="Dynamic" ToolTip="*Fill END Date!" />

<asp:Label ID="DateFilter" runat="server" 
Text="Filter by Date from :" style="font-family: 'Arial Narrow'" 
AssociatedControlID="StartDate"></asp:Label>
<asp:TextBox ID="StartDate" runat="server" ></asp:TextBox>
<a href="javascript:;" onclick="window.open('CalendarPopup.aspx?textbox=StartDate','cal','width=220,height=205,left=270,top=180')"><img id="Img1" 
src="calendar-icon.gif"  border="0" runat="server" alt="calendar" 
style="height: 23px" align="bottom"></a>

<asp:Label ID="Label4" runat="server" Text="to" Font-Names="Arial Narrow" 
AssociatedControlID="EndDate"></asp:Label>
<asp:TextBox ID="EndDate" runat="server"></asp:TextBox>
<a href="javascript:;" onclick="window.open('CalendarPopup.aspx?textbox=EndDate','cal','width=220,height=205,left=270,top=180')"><img id="Img2" src="calendar-icon.gif" border="0" runat="server" alt="calendar" style="height: 23px" dir="ltr"></a>&nbsp;
<asp:Button ValidationGroup="Search"
ID="Search" runat="server" BackColor="Black" Font-Names="Arial Narrow" 
ForeColor="White" Text="SEARCH" Width="73px" onclick="Search_Click" />

<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="EndDate" ControlToValidate="StartDate" 
ErrorMessage="CompareValidator" Operator="LessThan" Type="Date" 
ValidationGroup="Search" Display="Dynamic" SetFocusOnError="True">*First date must be less than or equal to Second date!</asp:CompareValidator>

~

于 2012-10-01T05:45:00.307 回答