[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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

0
Shepwedd
Asked:
Shepwedd
1 Solution
 
jmwheelerCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now