我编译时出错 [英] Error when i compile
本文介绍了我编译时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在运行它时遇到错误,它说输入字符串的格式不正确.有人可以帮忙吗,我已经尝试了一切
I get an error when I run it, it says Input string was not in a correct format. can someone please assist, I have tried everything
public partial class Test : System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
{
MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
MonthNumber = a
}
);
ddlMonth.DataBind();
ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
ddlYear.DataBind();
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
ddlday.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{ ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue))); ddlday.DataBind(); }
public bool IsPostBack { get; set; }
}
}
推荐答案
我认为是Convert.ToInt32(ddlMonth.SelectedValue); ...
``ddlMonth.SelectedValue''不是有效的整数字符串.
//先检查//
MessageBox.show(ddlMonth.SelectedValue.toString());
它将显示整数字符串.
确保这是一个有效的整数字符串.
I think in Convert.ToInt32(ddlMonth.SelectedValue); ...
''ddlMonth.SelectedValue'' is not a valid integer string.
//Check it first//
MessageBox.show(ddlMonth.SelectedValue.toString());
it will show integer string.
Make sure this is a valid integer string.
尝试一下此
Try this
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
{
MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
MonthNumber = a
//DataTextField = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
//DataValueField = a
}
);
//If this code is not working then try the above code in the loop.
ddlMonth.DataTextField="MonthName";
ddlMonth.DataValueField="MonthNumber";
ddlMonth.DataBind();
ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
ddlYear.DataBind();
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
ddlday.DataBind();
}
}
替换
ddlday.DataSource = Enumerable.Range(1,DateTime.DaysInMonth(DateTime.Now.Year,Convert.ToInt32(ddlMonth.SelectedValue))));
与
ddlday.DataSource = Enumerable.Range(1,DateTime.DaysInMonth(DateTime.Now.Year,Convert.ToInt32(ddlMonth.SelectedIndex + 1))));
我认为这可以解决您的问题.
Replace
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
with
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedIndex + 1)));
I think this will solve your issue.
这篇关于我编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文