C # asp.net button not displaying shortcut
I am creating a simple invoice calculator. When the count button is pressed, the label will display the total. For some reason, the button does nothing. I am not getting any errors in my code. Any help is appreciated.
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";
}
}
Ad
<asp:Button ID="btnCalc" runat="server" Text="Bill" />
<br />
<asp:Label ID="lblSummary" runat="server"></asp:Label>
source to share
It looks like your ASP.NET code section is missing OnClick.
<asp:Button ID="btnCalc" runat="server" Text="Bill" OnClick="btnCalc_Click"/>
Updated:
To fix another problem, instead of converting to decimal, try parsing as shown below,
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.
}
source to share
You need to tell the Button declaration which event handler to use. In this case, the event handler OnClick
, and the event function btnCalc_Click
.
Alternatively, add an OnClick event to that button declaration like this.
<asp:Button ID="btnCalc" runat="server" Text="Bill" OnClick="btnCalc_Click" />
OR
Provide an event to your button using C # code in an event page_load
like this.
btnCalc.Click += btnCalc_Click;
source to share