错误:输入字符串的格式不正确

时间:2013-12-01 21:02:27

标签: c# asp.net webforms

这是作业。当我运行此命令并单击页面底部的“添加自行车”按钮时,此行发生错误: BikeBinding b =此方法的新BikeBinding {

protected void btnAddBike_Click(object sender, EventArgs e)
        {
            BikeBinding b = new BikeBinding {
                manufacturer = Manufacturer.Text,
                gears = int.Parse((Gears).Text),
                frame = Frame.Text
            };

而不是int.Parse,我尝试使用Int32.Parse for Gears,但仍然有相同的错误。提前谢谢。

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

}

}

1 个答案:

答案 0 :(得分:3)

如果文本框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变量