C# 在文本框中输入自定义值时显示错误时出现问题

C# 在文本框中输入自定义值时显示错误时出现问题,c#,visual-studio-2010,visual-studio,visual-studio-2012,master-pages,C#,Visual Studio 2010,Visual Studio,Visual Studio 2012,Master Pages,我在Visual Studio 2012中工作,我在尝试通过一个文本框的标签显示一条简单的错误消息时遇到了困难,我已经创建了多年。例如,如果用户在txtYears文本框中输入“dfasdfsa”,则会显示错误“年份必须是数值”。为了更清楚,我将提供ID代表的内容 txtPrincliple = Priciple amount for a loan txtYears = duration of the loan in years lblResult = Result to the button c

我在Visual Studio 2012中工作,我在尝试通过一个文本框的标签显示一条简单的错误消息时遇到了困难,我已经创建了多年。例如,如果用户在txtYears文本框中输入“dfasdfsa”,则会显示错误“年份必须是数值”。为了更清楚,我将提供ID代表的内容

txtPrincliple = Priciple amount for a loan
txtYears = duration of the loan in years
lblResult = Result to the button click and the solution 
rblYears = radiobuttonList for duration in years
MonthlyPayment() = is a created method that returns the Monthly payment from provide input

  protected void Button1_Click(object sender, EventArgs e)
   {
    bool error = false;
    //Display Error if non numeric is entered
    if (!double.TryParse(txtPrinciple.Text, out principle))
    {
        error = true;
        lblResult.Text = "The principle must be a numeric value!";
    }

    //Get the values
    if (rblYears.SelectedIndex == 0)
        years = 15;
    else if (rblYears.SelectedIndex == 1)
        years = 30;
    else
        double.TryParse(txtYears.Text, out years);
    //Display Error if custom duration is entered
    if (!double.TryParse(txtYears.Text, out years))
    {
        error = true;
        lblResult.Text = "The years must be a numeric value!";
    }
    //Get interest rate value
    double.TryParse(ddlInterestRate.SelectedValue, out interest);
    //Output the Monthly Payment if no errors
    if (!error)
    {
        lblResult.Text = string.Format("Your total monthly payment is {0}{1:0.00}",   "$", MonthlyPayment());
    }
 }

据我所见,问题似乎来自这样一个事实,即即使用户从RBL中选择了某些内容,您也要进行错误验证。我不完全确定我是否理解这个问题,但是,它不是很清楚

如果你把它改成

//Get the values
    if (rblYears.SelectedIndex == 0)
        years = 15;
    else if (rblYears.SelectedIndex == 1)
        years = 30;
    else
    {
        //Display Error if custom duration is entered
        if (!double.TryParse(txtYears.Text, out years))
        {
            error = true;
            lblResult.Text = "The years must be a numeric value!";
        }
    }

我还删除了冗余的第一个TryParse(因为您在验证时这样做了)

您的问题是什么?我正试图用lblResult为textYears显示一条简单的错误消息。当用户从rblYears中选择15年或30年时,我似乎无法让错误消息起作用,它说“年份必须是数值!”我希望它进行计算。