在下拉列表中计算值

时间:2015-04-19 16:54:01

标签: c# asp.net switch-statement

我正在尝试在选择下拉列表的项目时计算值。 我试图使用开关这样做,但它不起作用。它始终为默认值,并且不会看到变量的值。

我正在从我的数据库构建动态下拉列表。

这是我到目前为止的代码:

protected void ddlClub_SelectedIndexChanged(object sender, EventArgs e)
    {
        // INSCHRIJFGELD BEREKENEN
        // variabelen voor berekenen inschrijfgeld
        double club1Price;
        club1Price = 1.25;
        double club2Price;
        club2Price = 5.50;
        double club3Price;
        club3Price = 9.25;
        double price;
        price = 20.00;

        int value;
        value = Convert.ToInt16(ddlClass.SelectedValue.ToString());

        switch (ddlClub.SelectedItem.Text)
        {
            case "club 1":
                if (Double.TryParse(lblAmount.Text, out club1Price)) 
                    club1Total = club1Price * value;
                    string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub2);
                break;
            case "club 2":
                if (Double.TryParse(lblAmount.Text, out club2Price)) 
                    culb2Total = club2Price * value;
                    string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub2);
                break;
            case "club 3":
                if (Double.TryParse(lblAmount.Text, out club3Price)) 
                    club3Total = club3Price * value;
                    string totalRoundClub3 = Convert.ToString(Math.Round(club3Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub3);
                break;
            default:
                if (Double.TryParse(lblAmount.Text, out price)) 
                    total = price * value;
                    string totalRound = Convert.ToString(Math.Round(total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRound);
                break;
        }
    }

我班级的变数:

public partial class Inschrijven : System.Web.UI.Page
{
    double club1Total;
    double club2Total;
    double club3Total;
    double total;

有人可以帮帮我吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

处设置断点
switch (ddlClub.SelectedItem.Text)

并检查ddlClub.SelectedItem.Text的值。我怀疑它不是你case陈述中的字符串之一。

我最好使用ddClub中的select项的有效性。例如:ddlClub.SelectedValue

让我们调试以下代码:

// the if statement is going to run line below it because there
// is not curly braces to execute a block of code
if (Double.TryParse(lblAmount.Text, out club1Price)) 
    club1Total = club1Price * value;

string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
lblAmount.Text = Convert.ToString(totalRoundClub2);

如果解析失败,Double.TryParse会将club1Price设置为ZERO。

应该写成以下内容:

if (Double.TryParse(lblAmount.Text, out club1Price))
{
    club1Total = club1Price * value;
}
else
{
    // the parse failed
    club1Price = 1.25;
    club1Total = club1Price * value;
}

string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
lblAmount.Text = Convert.ToString(totalRoundClub2);