Can't keep running totals - ASP.NET / VB
-
30-05-2021 - |
Pergunta
I have an assignment to create a website that people can use to make dinner reservations. We are using Visual Basic code for the forms. It's supposed to keep running totals of each type of dinner. I'm having trouble getting the running totals to not reset to zero. I have them declared outside of the button click event. Not sure where I went wrong. Here's the code:
Option Strict On
Partial Class _Default
Inherits System.Web.UI.Page
Private Chicken_RT As Integer
Private Rib_RT As Integer
Private Veg_RT As Integer
Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
'Variables
Dim DinnerPrice As Integer
Dim RadioSelect As Boolean = True
Dim Tickets As Integer
Dim DinnerType As String
'Parsing
Tickets = Integer.Parse(TicketTextBox.Text)
'Validation / Calculations
If TicketRangeValidator.IsValid And TicketValidator.IsValid Then
If ChickenRadio.Checked = True Then
DinnerPrice = Tickets * 15
Chicken_RT += Tickets
DinnerType = "Chicken"
ElseIf RibRadio.Checked = True Then
DinnerPrice = Tickets * 20
Rib_RT += Tickets
DinnerType = "Rib"
ElseIf VegetarianRadio.Checked = True Then
DinnerPrice = Tickets * 12
Veg_RT += Tickets
DinnerType = "Vegetarian"
Else
RadioErrorLabel.Text = ("Please select a dinner type")
RadioSelect = False
End If
'Display results
If RadioSelect = True Then
If OpenBarCheckBox.Checked = True Then
DinnerPrice += Tickets * 15
OpenBarLabel.Text = "Yes"
Else
OpenBarLabel.Text = "No"
End If
NumberDinnersLabel.Text = Tickets.ToString()
DinnerTypeLabel.Text = DinnerType
TotalCostLabel.Text = DinnerPrice.ToString("C")
TotalChickenLabel.Text = Chicken_RT.ToString()
TotalRibLabel.Text = Rib_RT.ToString()
TotalVegetarianLabel.Text = Veg_RT.ToString()
End If
End If
End Sub
End Class
Solução
As Joel said, the values will not persist as each HTTP request creates a new instance of your class. The HiddenField Class would suit you nicely in this situation.
First, remove these lines of code:
Private Chicken_RT As Integer
Private Rib_RT As Integer
Private Veg_RT As Integer
Second, put this in your .aspx Markup (nested in the Form tag):
<asp:HiddenField id="Chicken_RT" runat="server" value="0"/>
<asp:HiddenField id="Rib_RT" runat="server" value="0"/>
<asp:HiddenField id="Veg_RT" runat="server" value="0"/>
Finally,
Veg_RT += Tickets
Becomes:
Veg_RT.Value = (CInt(Veg_RT.Value) + Tickets).ToString()
(Repeat for all of your running totals.)
To access your totals you would do:
Dim intVegTotal As Integer = CInt(Veg_RT.Value)
Outras dicas
I have [the running totals] declared outside of the button click event.
Every time you handle an event, you're working with a new instance of your page class.
Understand that, and everything that's happening here should start to make sense to you. You must keep your totals in a place that persists across individual http requests, such as viewstate, the session, the application cache, on the file system, or in a database. Variables in your class to do not persist across individual http requests.
Well, I see you are reading TicketTextBox
, but I don't see you assigning it any where. If you don't assign it, won't it's value always be zero?