Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to calculate the total of price entered by users in textbox. Users need to enter the quantity in each textbox and then it will calculate with price that already display or show in label (quantity*price). The result will be showed in textbox readonly. 

What I have tried:

<blockquote class="quote"><div class="op">Quote:</div><h2> Product </h2>

    <th> Item </th>
    <th> Part Number </th>
    <th> Part Name </th>
    <th> Quantity </th>
    <th> Price (USD) </th>
    <th> Total (USD) </th>

                    <th> 1 </th>
                    <th> 485-3AB</th>
                    <th> Light ring </th>
                    <th> <asp:TextBox ID="txtbox1" runat="server" class="form-input"></asp:TextBox> </th>
                    <th>  <asp:Label ID="Label1" runat="server" Text="16.30"></asp:Label> </th>
                    <th  rowspan="2">  <asp:TextBox ID="txtTotal1" runat="server" class="form-input" ReadOnly="true"></asp:TextBox> </th>
                    <th> 456-2AA </th>
                    <th> Lens </th>
                    <th><asp:TextBox ID="txtbox2" runat="server" class="form-input"></asp:TextBox></th>
                    <th><asp:Label ID="Label2" runat="server" Text="0.29"> </asp:Label> </th>

                    <th> 2 </th>
                    <th> 985-BB</th>
                    <th> Charger </th>             
                    <th> <asp:TextBox  ID="txtbox3" runat="server" class="form-input"></asp:TextBox> </th>
                    <th> <asp:Label ID="Label3" runat="server" Text="6.50"></asp:Label> </th>
                    <th> <asp:TextBox ID="txtTotal2" runat="server" class="form-input" ReadOnly="true"></asp:TextBox> </th>

                    <th> 3 </th>
                    <th> 785-NM </th>
                    <th> Light Ring 3xl </th>
                    <th> <asp:TextBox  ID="txtbox4" runat="server" class="form-input"></asp:TextBox> </th>
                    <th>  <asp:Label ID="Label4" runat="server" Text="20.50"></asp:Label> </th>
                    <th>  <asp:TextBox ID="txtTotal3" runat="server" class="form-input" ReadOnly="true"></asp:TextBox> </th>

                    <th> 4 </th>
                    <th> M5-133 </th>
                    <th> Cable Type A </th>
                    <th> <asp:TextBox ID="txtbox5" runat="server" class="form-input"></asp:TextBox> </th>
                    <th> <asp:Label ID="Label5" runat="server" Text="3.10"></asp:Label> </th>
                    <th> <asp:TextBox ID="txtTotal4" runat="server" class="form-input" ReadOnly="true"></asp:TextBox> </th>

                    <th> 5 </th>
                    <th> M5-658 </th>
                    <th> Cable Type C </th>
                    <th> <asp:TextBox  ID="txtbox6" runat="server" class="form-input"></asp:TextBox> </th>
                    <th> <asp:Label ID="Label6" runat="server" Text="3.90"></asp:Label> </th>
                    <th> <asp:TextBox ID="txtTotal5" runat="server" class="form-input" ReadOnly="true"></asp:TextBox> </th>

            <asp:Button ID="Submitbtn" runat="server" Text="Submit" OnClick="Submitbtn_Click" /></blockquote>
Updated 27-Feb-23 16:23pm
Sandeep Mewara 20-Feb-23 1:27am    
And what's the issue? Seems you have your HTML designed and need to write the code behind on the Submit button click: Get the prices and quantities to have values of each item and then sum it up. Which step is where you are stuck?
kimkyie 20-Feb-23 1:55am    
I'm getting errors in my code behind such as :
The name 'Operators' does not exist in the current context
'MyDefault' is of type 'object'. A default parameter value of a reference type other than string can only be initialized with null
The name 'Conversions' does not exist in the current context
Sandeep Mewara 20-Feb-23 2:18am    
Seems you have copy-pasted the code behind from somewhere else. What is Operators? Would suggest you to start from writing code in C# for Hello World and then on how to access the values in textbox, etc.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

THis isn't difficult: get the strings the user entered, validate and convert each of them to numbers using the appropriate TryParse method for the value type involved (int.TryParse, double.TryParse etc.), then perform the sum required. Convert the result to a string and send it back to the client. What's the problem?

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
kimkyie 20-Feb-23 1:53am    
I've add my code behind it having error and I don't know how to fix it
OriginalGriff 20-Feb-23 2:32am    
"The name 'Operators' does not exist in the current context"
Since we have no idea what the "Operators" class is, or where it might be defined, we can't help you fix that - the system is saying exactly the same thing: "I don't know what this class is"
So look at where you wrote the class and find out why the system can't access it.

And why write a method to replace "x * y" anyway? It's certainly not clearer than the built-in multiplication operator ... :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900