allendks45
allendks45

Reputation: 341

How can I aggregate asp.net form checkbox data with Calculate Button

New challenge for beginner. Been playing with asp.net using VS-2012 IDE. Managed to create this simple order sheet and everything works except I cannot find an answer how to store then total the cost of 4-checkboxes on the form. A user can select either one or all of them and when the button event is called it should put the data into a label on the form called Options Cost. Ideally I want to keep the code in VB. Also, apologize if I do not respond to your answers immediately (will not have ability to check until later.

asp.net code:

<%@ Page Language="VB" AutoEventWireup="false"   
CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:Label ID="Label2" runat="server" Text="Computer Purchase 
Build Sheet"></asp:Label>
    <br />
    <br />
    <asp:Label ID="Num1" runat="server" Text="Base Computer Cost 
$1,500.00"></asp:Label>

</div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Num2" runat="server" Text="Additional 4GB Memory 
Chips $70.00"></asp:Label>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Num3" runat="server" EnableTheming="True" 
Text="Additional Hard Drives (500GB) $90.00"></asp:Label>
    <br />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Num4" runat="server" EnableTheming="True"   
Text="Additonal Hard Drives (1TB) $130.00"></asp:Label>
    <br />
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Option Packages">
</asp:Label>
    <br />
    <br />
    <asp:CheckBox ID="CheckBox1" runat="server" Text="24&quot; Monitor 
$175" />
    <br />
    <br />
    <asp:CheckBox ID="CheckBox2" runat="server" Text="36&quot; Monitor 
$400" />
    <br />
    <br />
    <asp:CheckBox ID="CheckBox3" runat="server" Text="Boss Headset $100" 
/>
    <br />
    <br />
    <asp:CheckBox ID="CheckBox4" runat="server" Text="Speaker System 
$200" />
    <br />
    <br />
    <asp:Label ID="Num5" runat="server" Text="Base Cost:"></asp:Label>
&nbsp;&nbsp;
    <asp:Label ID="LblAns1" runat="server" Text="?"></asp:Label>
    <br />
    <asp:Label ID="Num6" runat="server" Text="Options Cost:"></asp:Label>

&nbsp;&nbsp;
    <asp:Label ID="LblAns2" runat="server" Text="?"></asp:Label>
    <br />
    <asp:Label ID="Num7" runat="server" Text="Tax Due:"></asp:Label>

&nbsp;&nbsp;
    <asp:Label ID="LblAns3" runat="server" Text="?"></asp:Label>
    <br />
    <asp:Label ID="Num8" runat="server" Text="Grand Total:"></asp:Label>

&nbsp;
    <asp:Label ID="LblAns4" runat="server" Text="?"></asp:Label>
    <br />
    <br />
    <asp:Button ID="BtnCalc" runat="server" Text="Calculate" />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="BtnClear" runat="server" Text="Clear" Width="82px" />
    <br />
    <br />
    <br />
    <br />
    <br />
</form>
</body>
</html>

VB Code:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub BtnCalc_Click(sender As Object, e As EventArgs) Handles   
BtnCalc.Click
    Dim Num1, Num2, Num3, Num4 As Double
    Dim Options As Double
    Dim Ans1, tax, finalCost As Double

    Num1 = Val(TextBox1.Text) * 1500
    Num2 = Val(TextBox2.Text) * 70
    Num3 = Val(TextBox3.Text) * 90
    Num4 = Val(TextBox4.Text) * 130

    ' Get checkboxlist data


    Ans1 = Num1 + Num2 + Num3 + Num4
    tax = Ans1 * 0.095
    finalCost = Ans1 + tax

    LblAns1.Text = Ans1
    LblAns3.Text = tax
    LblAns4.Text = finalCost
End Sub

Protected Sub BtnClear_Click(sender As Object, e As EventArgs) Handles 
BtnClear.Click
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    LblAns1.Text = ""
    LblAns2.Text = ""
    LblAns3.Text = ""
    LblAns4.Text = ""
End Sub

End Class

Upvotes: 0

Views: 85

Answers (1)

Carrie Davis
Carrie Davis

Reputation: 61

You can just check each check box to see if it is checked or not. Like this:

Dim iTotal as Integer = 0

If Checkbox1.Checked Then
  iTotal += 175
End If

Then, you can display your total in the label.

Upvotes: 1

Related Questions