开发者

help in compare validate in c#

开发者 https://www.devze.com 2022-12-26 20:43 出处:网络
i have 2 textboxes that i am fil开发者_StackOverflowling with StartDate, and Endate. i want to use the compareValidator to make sure that the StartDate is less that the EndDate.i used this:

i have 2 textboxes that i am fil开发者_StackOverflowling with StartDate, and Endate.

i want to use the compareValidator to make sure that the StartDate is less that the EndDate. i used this:

 <asp:CompareValidator ID="Comp" ControlToValidate="txtStartDate" ControlToCompare="txtEndDate" Operator="LessThan" Type="Date" runat="server" Display="dynamic" Text="Failed!" />

the validator is always giving me Failed no matter what the dates are. if startdate>endate or vise versa, Failed appears.

i am using (MM/dd/yyyy) format in the textboxes.

EDIT: ok i solved the problem. if anyone interested here is the new comparevalidator:

 <asp:CompareValidator ID="Comp" ControlToValidate="txtEndDate" ControlToCompare="txtStartDate" Operator="GreaterThan" Type="Date" runat="server" Display="dynamic" Text="Failed!" />


The trick here is the ControlToValidate and ControlToCompare.

in your case the ControlToValidate should be the textbox that has the start date and ControlToCompare should have bthe textbox that has the end date. sometimes people confuse them.

i used this code to check

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ErrorMessage="start date should be less than end date" ControlToCompare="TextBox2" 
            ControlToValidate="TextBox1" Display="Dynamic" Operator="LessThan" Type="Date"></asp:CompareValidator>

i hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号