Surya sasidhar
Surya sasidhar

Reputation: 30313

Compare Validator in asp.net?

In web application, i am try to validate the date by using compare validator. but it is throwing error, This is my code

<asp:CompareValidator 
               ID="CompareValidator1" 
               runat="server" 
               Text="*" 
               Display="None" 
               ValidationGroup ="a" 
               ControlToValidate="txtValidate" 
               ValueToCompare='<%# DateTime.Today.ToShortDateString() %>'
               Operator="GreaterThan"   
               Type ="Date" 
               ErrorMessage="Date Should Greater Than Todate">
</asp:CompareValidator>

The error is :

The value '' of the ValueToCompare property of 'CompareValidator1' cannot be converted to type 'Date'.

Upvotes: 0

Views: 3470

Answers (3)

Neha
Neha

Reputation: 2965

In your Page_Load method, call Page.DataBind().

This will execute the databinder code when the page is loaded.

Upvotes: 2

Chris
Chris

Reputation: 7369

Try '=' instead of '#'. See here

<asp:CompareValidator 
               ID="CompareValidator1" 
               runat="server" 
               Text="*" 
               Display="None" 
               ValidationGroup ="a" 
               ControlToValidate="txtValidate" 
               ValueToCompare='<%= DateTime.Today.ToShortDateString() %>'
               Operator="GreaterThan"   
               Type ="Date" 
               ErrorMessage="Date Should Greater Than Todate">
</asp:CompareValidator>

Upvotes: -1

Ashfaq Shaikh
Ashfaq Shaikh

Reputation: 1668

Where is your Comapre Validator, In Grid or Repeater or it just on page.

if it is not in Grid then you can give ValueToCompare value from code behind. ValueToCompare='<%# DateTime.Today.ToShortDateString() %>' not actually bind the date. you can check it using fire bug. it not render as date it will display you as string format. so kindly set this value from code behind. use this code.

 protected void Page_Load(object sender, EventArgs e)
        {
    CompareValidator1.ValueToCompare = DateTime.Today.ToShortDateString();
    }

Upvotes: 1

Related Questions