C#asp.net按钮单击不显示标签

时间:2015-04-20 23:30:56

标签: c# asp.net button label

我正在创建一个简单的账单计算器。单击“帐单”按钮时,标签将显示总计。由于某种原因,该按钮不起任何作用。我的代码中没有出现任何错误。任何帮助表示赞赏。

    protected void btnCalc_Click(object sender, EventArgs e)
{
    Validate();
    if (IsValid)
    {
    }

    decimal tips = Convert.ToDecimal(txtTips.Text);
    decimal meals = Convert.ToDecimal(txtMeals.Text);
    decimal buffets = Convert.ToDecimal(txtBuffets.Text);
    string fname = txtFirstName.Text;
    string tnumber = txtTableNumber.Text;
    decimal mealsvalue = meals * 16.99M;
    decimal buffetsvalue = buffets * 11.5M;
    decimal tax = 0;
    decimal walkR = 0;
    decimal phoneR = 0;

    if (rblTax.SelectedValue == "Tax")
    {
       tax = .06M;
    }
    else
    {
    }


    if (rblReservation.SelectedValue == "Walk-in")
    {
        decimal subtotal = walkR + mealsvalue + buffetsvalue;
        decimal taxvalue = subtotal * tax;
        decimal total = walkR + mealsvalue + buffetsvalue + taxvalue + tips;
        lblSummary.Text = "First name is " + fname + " Table number is " + tnumber + " Walk in cost is " + walkR + " Cost of meals is " + mealsvalue +
        " Cost of buffets is " + buffetsvalue + " Tax is " + taxvalue + " Tip is " + tips + " Total is " + total;
    }
    else if (rblReservation.SelectedValue == "Phone")
    {
        phoneR = 3;
        decimal subtotal = phoneR + mealsvalue + buffetsvalue;
        decimal taxvalue = subtotal * tax;
        decimal total = phoneR + mealsvalue + buffetsvalue + tax + tips;
        lblSummary.Text = "First name is " + fname + " Table number is " + tnumber + " Phone reservation cost is " + phoneR + " Cost of meals is " + mealsvalue +
        " Cost of buffets is " + buffetsvalue + " Tax is " + tax + " Tip is " + tips + " Total is " + total;
    }
    else
    {
        lblSummary.Text = "Please fill out the information";
    }
}

按钮声明

<asp:Button ID="btnCalc" runat="server" Text="Bill" />
    <br />
    <asp:Label ID="lblSummary" runat="server"></asp:Label>

2 个答案:

答案 0 :(得分:3)

在代码的ASP.NET部分中看起来缺少OnClick。

<asp:Button ID="btnCalc" runat="server" Text="Bill" OnClick="btnCalc_Click"/>

更新:

要修复其他问题,请尝试解析,而不是转换为十进制,

decimal tips = 0;
bool result = decimal.TryParse(txtTips.Text, out tips);

if (result)
{
 //txtTips.Text has a valid decimal value. You can proceed with your logic.
}

答案 1 :(得分:0)

您需要告诉Button声明使用哪个事件处理程序。 在这种情况下,事件处理程序为OnClick,事件函数为btnCalc_Click

在你的按钮声明中添加OnClick事件就像这样。

<asp:Button ID="btnCalc" runat="server" Text="Bill" OnClick="btnCalc_Click" />

OR

使用page_load这样的事件中的C#代码为您的按钮提供事件。

btnCalc.Click += btnCalc_Click;