title: How to Use a Compare Validator with DateTime.Today against TextBox.Text?
body: <p><strong>Goal:</strong>
  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).</p>
  
  <p>I've tried two methods:</p>
  
  <p><strong>Method One</strong></p>
  
  <pre><code>&lt;asp:TextBox ID="HiddenTodayDate" Visible = "false" runat="server" /&gt;
  &lt;asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date" 
       ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox" 
       ErrorMessage="'End Date' must be before today's date" runat="server" /&gt;
  </code></pre>
  
  <p>And the following in my Page_Load method:</p>
  
  <pre><code>HiddenTodayDate.Text = DateTime.Today.ToShortDateString();
  </code></pre>
  
  <p><strong>Method Two</strong></p>
  
  <pre><code>&lt;asp:HiddenField ID="HiddenTodayDate" runat="server" /&gt;
  &lt;asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date" 
       ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox" 
       ErrorMessage="'End Date' must be before today's date" runat="server" /&gt;
  </code></pre>
  
  <p>And the following in my Page_Load method:</p>
  
  <pre><code>HiddenTodayDate.Value = DateTime.Today.ToShortDateString();
  </code></pre>
  
  <p>To the code savvy obviously setting a <code>TextBox</code> visible to false prevents the <code>Validator</code> 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:</p>
  
  <blockquote>
    <p>Control <code>HiddenTodayDate</code> referenced by the <code>ControlToValidate property</code>
    of `CompareEndTodayValidator cannot be validated.</p>
    
    <p>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. </p>
    
    <p>Exception Details: System.Web.HttpException: Control 'HiddenTodayDate'
    referenced by the ControlToValidate property of
    'CompareEndTodayValidator' cannot be validated.</p>
    
    <p>Source Error:</p>
    
    <p>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.</p>
  </blockquote>
  
  <p>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!</p>