Reputation:
I am using AJAX calender extender with a textbox, And i want to disable past dates, so that user do not able to select those, which are less than today's date. My code is as follows
In .aspx page
<asp:TextBox ID="txtFromDate" runat="server" ontextchanged="txtFromDate_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtFromDate">
</asp:CalendarExtender>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
and on .cs page
protected void txtFromDate_TextChanged(object sender, EventArgs e)
{
DateTime dt1 = DateTime.Parse(txtFromDate.Text);
if (dt1 < DateTime.Now)
{
//Response.Write("you can not choose date earlier than today's date");
txtFromDate.Text = null;
}
}
But i am getting the following error:
System.FormatException: String was not recognized as a valid DateTime.
and is there any way that i can make those date unclickable so that user cant choose from them using startDate and Enddate attributes, or by some other means, I also tried those but again i got error that these are not supported. Any help will be appreciated.
Upvotes: 1
Views: 4335
Reputation: 14243
Try this if your Date
's format is "yyyy/MM/dd":
String[] date = typedDate.Text.Split(new char[] { '/' });
Datetime dy = new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]));
Upvotes: 1