【发布时间】:2014-08-01 21:53:05
【问题描述】:
在我的 asp.net c# 应用程序中,我使用 ajax 日历扩展器来收集日期时间信息。当我尝试提交此页面时,它显示字符串未被识别为有效日期时间。我尝试了不同的转换方法,但没有一种方法适合我。
string date =Request.Form[ txtBookDate.UniqueID];
DateTime yeer = Convert.ToDateTime(date);
字符串未被识别为有效的日期时间。
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详细信息:System.FormatException:无法识别字符串 作为有效的日期时间。
来源错误:
第 78 行:字符串日期 =Request.Form[txtBookDate.UniqueID]; 第 79 行:第 80 行:日期时间 yeer = 转换.ToDateTime(日期);第 81 行:日期时间预订日期 = Convert.ToDateTime(date.ToString());第 82 行:int WedYear = Bookdate.Year;
源文件:D:\Sample Project\AuditoriumNew\AuditoriumNew\Registration.aspx.cs 行:80
堆栈跟踪:
[FormatException:字符串未被识别为有效的 DateTime。]
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles 样式)+10404058 System.Convert.ToDateTime(String 值)+83 AuditoriumNew.Index.txtRent_TextChanged(对象发送者, D:\Sample 中的 EventArgs e) 项目\AuditoriumNew\AuditoriumNew\Registration.aspx.cs:80
System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +116
System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +50
System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10 System.Web.UI.Page.RaiseChangedEvents() +132 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +1644版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.17929
ajax Extender 的源代码
<asp:Label ID="bok" Text="Booking Date" runat="server"></asp:Label>
<asp:TextBox ID="txtBookDate" runat="server" ReadOnly="true"></asp:TextBox>
<asp:Image ID="calender" runat="server" ImageUrl="img/cals/calender.png" />
<asp:CalendarExtender ID="BookingDate" TargetControlID="txtBookDate" PopupButtonID="calender"
runat="server" PopupPosition="TopRight" DaysModeTitleFormat="dd/MM/yyyy"
TodaysDateFormat="dd/MM/yyyy">
</asp:CalendarExtender>
实际代码
protected void txtRent_TextChanged(object sender, EventArgs e)
{
float CentralAmnt;
float renter;
double rent = Convert.ToDouble(txtRent.Text);
renter = float.Parse(rent.ToString());
///Calculate CTValue
double CTValue = Convert.ToDouble(12.36 / 100);
double CTax = CTValue;
float CentralTax = float.Parse(CTax.ToString());
CentralAmnt = renter * CentralTax;
///Calculate LT Value
double LTax = 0.15;
float LuxTax = float.Parse(LTax.ToString());
float LuxAmnt = renter * LuxTax;
string s = txtBookDate.Text;
DateTime yeer;
yeer = Convert.ToDateTime(s);
//DateTime yeer = Convert.ToDateTime(s);
DateTime Bookdate = Convert.ToDateTime(yeer.ToString());
int WedYear = Bookdate.Year;
if (WedYear < 2014)
{
float TotAmnt = renter + LuxAmnt;
txtTotal.Text = TotAmnt.ToString();
LuxuaryTaxAmnt = LuxAmnt;
}
else
{
float TotAmnt = renter + CentralAmnt + LuxAmnt;
txtTotal.Text = TotAmnt.ToString();
LuxuaryTaxAmnt = LuxAmnt;
CentralTaxAmnt = CentralAmnt;
}
}
【问题讨论】:
-
放一个断点,看看你在
date字符串中得到了什么值。 -
不,@Habib 我认为我们应该自己判断这个价值:-)
-
您能提供您的 ASPX 页面代码吗?
-
调试 20 个问题。为什么人们很难帮助你?在您的问题中包含所有相关信息。就目前而言,您的问题甚至不是问题。这只是一个悲伤的故事的叙述。
-
确保 1:提供重现问题的完整示例(即
DateTime.Parse("YerExactTextHere")),2:在发布您的问题之前阅读一些类似的问题stackoverflow.com/…。
标签: c# asp.net ajax datetime asp.net-ajax