How do I use a DateTime variable in C#

I have written the attached c# code to display todays date in a textbox (TodayDateEditTextBox) that is located in the EditItemTemplate of an asp.net 3.5 listview control. I have another textbox (DueDateEditTextBox) which has an ajax CalendarExtender attached to it to allow the user to select a date. I want to use a CompareValidator to make sure that the selected date is greater than todays date. The CompareValidator code that I have used is:

<asp:CompareValidator ID="DateValidator" runat="server" ControlToCompare="TodayDateEditTextBox" ControlToValidate="DueDateEditTextBox" ErrorMessage="<br><small>Date must be after today.</small>" Operator="GreaterThan" ValidationGroup="AccountUpdate"></asp:CompareValidator>

Unfortunately this only works if the day part of the date is greater than todays date, i.e. the validator error is thrown if todays date is 21/04/2009 and the user selects 20/04/2009 - which is fine, but if todays date is 21/04/2009 and the user selects 01/05/2009 the error still flags??




protected void lvAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (lvAccounts.EditIndex >= 0)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
 
            if (dataItem.DisplayIndex == lvAccounts.EditIndex)
            {
                TextBox tbTodayDateEdit = (TextBox)dataItem.FindControl("TodayDateEditTextBox");
                
                //Fills this textbox with today's date to validate that the Due Date is after today's date
                tbTodayDateEdit.Text = DateTime.Today.ToShortDateString();
             }
         }
    }

Open in new window

ShepweddAsked:
Who is Participating?
 
jmwheelerConnect With a Mentor Commented:
Add  Type="Date" to your validator.
<asp:CompareValidator ID="DateValidator" runat="server" ControlToCompare="TodayDateEditTextBox" Type="Date" ControlToValidate="DueDateEditTextBox" ErrorMessage="<br><small>Date must be after today.</small>" Operator="GreaterThan" ValidationGroup="AccountUpdate"></asp:CompareValidator>

Open in new window

0
 
ShepweddAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.