簡體   English   中英

System.FormatException:字符串未被識別為有效的DateTime

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

我正在使用帶有文本框的AJAX日歷擴展器,並且我想禁用過去的日期,以便用戶無法選擇那些比今天的日期短的日期。 我的代碼如下

在.aspx頁中

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

並在.cs頁上

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;
        } 
    }


但我收到以下錯誤:
System.FormatException:字符串未被識別為有效的DateTime
並且有什么方法可以使這些日期不可點擊,以便用戶無法使用startDate和Enddate屬性從中進行選擇,或者通過其他某種方式,我也嘗試了這些方法,但再次出現錯誤,提示這些方法不受支持。 任何幫助將不勝感激。

如果您的Date的格式為“ 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]));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM