简体   繁体   中英

System.FormatException: String was not recognized as a valid DateTime

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:
.
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.

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]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM