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

C# asp.net button click does not display 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>

您的 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.
}

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

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

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

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

btnCalc.Click += btnCalc_Click;