I have a dilemma with two formatted TextBoxes that receive their text value from a CalendarExtender. I need to validate that the first TextBox is greater than the second one, but the values are coming in as strings rather than dates. How can I go about validating this? Here is my ASP code:
<asp:TextBox ID="TextBox1" runat="server" style="width:160px; text-align:center;" OnServerValidate="DateRange_ServerValidate"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" style="width:160px; text-align:center;" OnServerValidate="DateRange_ServerValidate"></asp:TextBox>
<asp:Label ID="lblDateError" runat="server" ForeColor="#CC0000" ></asp:Label>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="dddd, MMMM dd yyyy"
TargetControlID="TextBox1" PopupButtonID="Image1">
</asp:CalendarExtender>
<asp:CalendarExtender ID="CalendarExtender2" runat="server" Format="dddd, MMMM dd yyyy"
TargetControlID="TextBox2" PopupButtonID="Image4">
</asp:CalendarExtender>
Regarding the code behind:
protected void DateRange_ServerValidate(object sender, EventArgs args)
{
DateTime ToDate = DateTime.ParseExact(TextBox1.Text.ToString(), "dddd, MMMM dd yyyy", CultureInfo.InvariantCulture);
DateTime currentdate = DateTime.ParseExact(TextBox2.Text.ToString(), "dddd, MMMM dd yyyy", CultureInfo.InvariantCulture);
if (ToDate < currentdate)
{
lblDateError.Visible = true;
lblDateError.Text = "End Date should not be earlier than the current date.";
return;
}
else
{
lblDateError.Text = "";
}
}
Any assistance would be greatly appreciated!