CompareValidator for dates > than today

T

tshad

I am trying to check the date of my field to check if it is greater that
today, but I am getting an error.

I am using :

<asp:CompareValidator runat="server"
ControlToCompare="RequestedStartDate"
Operator="GreaterThan"
Display="Dynamic"
ValueToCompare = DateTime.Today.ToShortDateString()
Type="Date" text="<br>Invalid Start Date"/>

But it gives me an error:

The value 'DateTime.Today()' of the ValueToCompare property of '' cannot
be converted to type 'Date'

I also tried DateTime.Today(), but got the same error. How would I do this?

Thanks,

Tom
 
T

tdavisjr

tshad said:
I am trying to check the date of my field to check if it is greater that
today, but I am getting an error.

I am using :

<asp:CompareValidator runat="server"
ControlToCompare="RequestedStartDate"
Operator="GreaterThan"
Display="Dynamic"
ValueToCompare = DateTime.Today.ToShortDateString()
Type="Date" text="<br>Invalid Start Date"/>

But it gives me an error:

The value 'DateTime.Today()' of the ValueToCompare property of '' cannot
be converted to type 'Date'

I also tried DateTime.Today(), but got the same error. How would I do this?

Thanks,

Tom

How about this:

ValueToCompare = '<%= DateTime.Today.ToShortString() %>'

put a semi-colon to the end of the ToShortString() if using C#. See if
that works
 
T

tshad

tdavisjr said:
How about this:

ValueToCompare = '<%= DateTime.Today.ToShortString() %>'

put a semi-colon to the end of the ToShortString() if using C#. See if
that works
That was what I was looking for.

Thanks,

Tom
 
Joined
Apr 15, 2007
Messages
1
Reaction score
0
Dates less than current date

I'm using:

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox7"
Display="Dynamic" ErrorMessage="CompareValidator" Operator="LessThan" Type="Date"
ValueToCompare = '<%= DateTime.Today.ToShortString() %>'></asp:CompareValidator>

I am getting:

The value '<%= DateTime.Today.ToShortString() %>' of the ValueToCompare property of 'CompareValidator1' cannot be converted to type 'Date'.

Can someone please help me?
 
Joined
Aug 7, 2007
Messages
1
Reaction score
0
Did you try to replace where you had:

ValueToCompare = '<%= DateTime.Today.ToShortString() %>'

With

ValueToCompare="<%# DateTime.Today.ToShortDateString() %>"


This worked for me

PS - You also need to call Page.DataBind() in Page_Load for this syntax to work
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top