验证器即使使用IE也无法在任何浏览器中工作

时间:2012-10-01 05:20:30

标签: asp.net visual-studio-2008 c#-4.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" />

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

<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>

两个验证器都不起作用。有人请帮忙。谢谢..:) 丝提

1 个答案:

答案 0 :(得分: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>