简体   繁体   English

CompareValidator 工作不正常 - ASP.net

[英]CompareValidator not working well - ASP.net

I have used compare validator to check whether the selected date is valid or it.我已经使用比较验证器来检查所选日期是否有效。 The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.这里的问题是它仅在单击提交按钮时触发,是否可以检查用户何时选择日期。

<tr id="trow" runat="server"> 
            <td class="auto-style3">Need Duration</td>  
            <td class="auto-style2">  
            <asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
             <asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
            ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red" 
            SetFocusOnError="True"></asp:RequiredFieldValidator></td>


              <td> 
             <asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
              <asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
             <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
            ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red" 
            SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
              ControlToValidate="TextBox2" ControlToCompare="TextBox1" 
               ErrorMessage='Invalid Date' 
                 ForeColor="Red"></asp:CompareValidator>
             </td>
            </tr>  

It has been a while, but I think you need to enable client side validation scripts by adding:已经有一段时间了,但我认为您需要通过添加以下内容来启用客户端验证脚本:

EnableClientScript="True"

Example例子

<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server" 
          Operator="GreaterThanEqual"
          ControlToValidate="TextBox2" ControlToCompare="TextBox1" 
          ErrorMessage='Invalid Date' 
          ForeColor="Red"></asp:CompareValidator>

It's documented at msdn .它记录在msdn


Aditionally, I do know that custom validators often lack a correct implementation of the javascript.另外,我确实知道自定义验证器通常缺乏 javascript 的正确实现。 I am not sure how the CompareValidator behaves in that sense.我不确定CompareValidator在这个意义上的表现。

You might need to create a inherited class, to implement the scripts fully.您可能需要创建一个继承类,以完全实现脚本。 Before going there, try do research a bit.在去那里之前,试着做一些研究。

For example, here is a solution with a custom validator例如,这是一个带有自定义验证器的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM