Reputation: 3315
Goal: I have an 'EndDate' TextBox that updates upon the user changing it. I want to be able to check/validate the date in EndDateTextBox.Text and make sure it is less than today's date (in format ex: 4/19/2013).
I've tried two methods:
Method One
<asp:TextBox ID="HiddenTodayDate" Visible = "false" runat="server" />
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date"
ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox"
ErrorMessage="'End Date' must be before today's date" runat="server" />
And the following in my Page_Load method:
HiddenTodayDate.Text = DateTime.Today.ToShortDateString();
Method Two
<asp:HiddenField ID="HiddenTodayDate" runat="server" />
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date"
ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox"
ErrorMessage="'End Date' must be before today's date" runat="server" />
And the following in my Page_Load method:
HiddenTodayDate.Value = DateTime.Today.ToShortDateString();
To the code savvy obviously setting a TextBox
visible to false prevents the Validator
from seeing it as well, but I didn't know it at the time and wanted to document my process. When I try method two, I come across the following error when debugging:
Control
HiddenTodayDate
referenced by theControlToValidate property
of `CompareEndTodayValidator cannot be validated.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control 'HiddenTodayDate' referenced by the ControlToValidate property of 'CompareEndTodayValidator' cannot be validated.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
So is there a means to somehow achieve my goal without having to display DateTime.Today somewhere? I'd prefer to keep my code as simple and clean as possible and not use Javascript, but if Javascript provides the only workaround, then so be it. Code would be greatly appreciated. Thanks!
Upvotes: 4
Views: 32109
Reputation: 460048
You can set the ValueToCompare
property programmatically to today:
<asp:comparevalidator runat="server" Id="CompareEndTodayValidator"
errormessage="The date must be less than today"
controltovalidate="EndDate" type="Date" Operator="LessThan"
ValueToCompare="<%= DateTime.Today.ToShortDateString() %>" />
(not tested, if <%=
doesn't work use <%#
, then you have to remember to call Page.DataBind()
somewhere(f.e. in Page_Load
) if it is not in a databound context)
Upvotes: 4
Reputation: 3315
After learning of the the ValueToCompare property due in part to Tim's post I was able to search around and find a question similar to mine with an answer that almost worked (had to change ASP.NET compare type to String): Using the CompareValidator control to compare user input date with today's date
Here's what my code looks like:
ASP.NET:
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="LessThan" type="String" ControltoValidate="EndDateTextBox" ErrorMessage="The 'End Date' must be before today" runat="server" />
.NET:
(In Page_Load method)
CompareEndTodayValidator.ValueToCompare = DateTime.Now.ToShortDateString();
Upvotes: 9