[英]Error: Input string was not in a correct format
这是功课。 当我运行此命令并单击页面底部的“添加自行车”按钮时,此行将发生错误: BikeBinding b = new BikeBinding {此方法:
protected void btnAddBike_Click(object sender, EventArgs e)
{
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = int.Parse((Gears).Text),
frame = Frame.Text
};
我尝试使用Gears的Int32.Parse而不是int.Parse,但仍然有相同的错误。 提前致谢。
using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MvcApplication3
{
public partial class WebFormBinding : System.Web.UI.Page
{
List<BikeBinding> bicycles;
public void Page_Load(object sender, EventArgs e)
{
bicycles = GetApplicationBicycles();
FormView1.DefaultMode = FormViewMode.Edit;
BindControls();
// Page.DataBind
// FormView1.DefaultMode = FormViewMode.Edit;
}
private void BindControls()
{
// Bind all DataBound Controls
foreach (Control wc in PanelDataBoundControls.Controls)
{
if (wc is DataBoundControl)
{
DataBoundControl lc = (DataBoundControl)wc;
// Response.Write("<br />" + lc.ToString());
lc.DataSource = bicycles;
lc.DataBind();
}
}
// Not DataBound Controls, so I'll bind them by hand
DataList1.DataSource = bicycles;
DataList1.DataBind();
Repeater1.DataSource = bicycles;
Repeater1.DataBind();
}
private List<BikeBinding> GetApplicationBicycles()
{
return (List<BikeBinding>)Application["bicycles"];
}
protected void btnAddBike_Click(object sender, EventArgs e)
{
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = int.Parse((Gears).Text),
frame = Frame.Text
};
bicycles.Add(b);
// Saves the bicycle list to the Application object
Application.Lock();
Application["bicycles"] = bicycles;
Application.UnLock();
Manufacturer.Text = String.Empty;
Gears.Text = String.Empty;
Frame.Text = String.Empty;
BindControls();
}
}
}
命名空间MvcApplication3 {
public class MvcApplication : System.Web.HttpApplication
{
List<BikeBinding> bicycles;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
bicycles = new List<BikeBinding>();
BikeBinding b = new BikeBinding { bikeID = 0, manufacturer = "Schwinn", gears = 10, frame = "Racing" };
bicycles.Add(b);
b = new BikeBinding { bikeID = 1, manufacturer = "Nishiki", gears = 5, frame = "Mountain" };
bicycles.Add(b);
b = new BikeBinding();
b.bikeID = 2;
b.manufacturer = "Trek Medone";
b.gears = 7;
b.frame = "Road";
for (int i = 0; i < 3; i++)
{
BikeBinding x = new BikeBinding { bikeID = i, manufacturer = i.ToString(), gears = i, frame = i.ToString() };
bicycles.Add(x);
}
// Saves the bicycle list to the Application object
Application.Lock();
Application["bicycles"] = bicycles;
Application.UnLock();
}
}
}
如果Gears
文本框包含无法转换为整数的值,则Parse
方法将引发异常。 因此,您需要更改代码以处理这种情况
protected void btnAddBike_Click(object sender, EventArgs e)
{
int g;
if(!Int32.TryParse(Gears.Text, out g))
{
// some label where you could write the error message
// labelError.Text = "An integer number is required.....";
return;
}
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = g,
frame = Frame.Text
};
bicycles.Add(b);
.....
如果无法将文本转换为整数,则TryParse方法将返回false,而不是引发异常。 如果可以转换,则将值分配给作为第二个参数传递给TryParse的out变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.