Calculate total in C# then output to a table

Kris Ellison 25 Reputation points
2023-04-25T08:36:11.1966667+00:00

IScreenshot (289)

I am trying to sum the subtotal and total columns but not sure how to. I've tried accessing the quantity and price to perform the calculation but haven't been able to work it out. Below is a snippet of my C# and my table I am trying to output it to. Any advice would be greatly appreciated.

Print.aspx.cs

String.IsNullOrEmpty(Request.Form["txtQuantity1"]))
            {
                lblQuantity1.Text = Request.Form["txtQuantity1"];
            {
         
if (!String.IsNullOrEmpty(Request.Form["txtPrice1"]))
            {
                lblPrice1.Text = Request.Form["txtPrice1"];

Print.aspx
             <tr>
                 <td><asp:Label ID="lblItem1" runat="server" Text="--"></asp:Label></td>
                 <td><asp:Label ID="lblPrice1" runat="server" Text="--"></asp:Label></td>
                 <td><asp:Label ID="lblQuantity1" runat="server" Text="--"></asp:Label></td>
                 <td><asp:Label ID="Total1" runat="server" Text="--"></asp:Label></td>
             </tr>
Developer technologies ASP.NET Other
Developer technologies C#
{count} vote

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-04-26T09:43:02+00:00

    Hi @Kris Ellison,

    The value of the label can be obtained directly for calculation.

    Place the following code at the end of Page_Load in Print.aspx.cs.

     int total1 = Convert.ToInt32(lblQuantity1.Text.ToString()) + Convert.ToInt32(lblPrice1.Text.ToString());
                    Total1.Text = total1.ToString();
                    int total2 = Convert.ToInt32(lblQuantity2.Text.ToString()) + Convert.ToInt32(lblPrice2.Text.ToString());
                    Total2.Text = total2.ToString();
                    int total3 = Convert.ToInt32(lblQuantity3.Text.ToString()) + Convert.ToInt32(lblPrice3.Text.ToString());
                    Total3.Text = total3.ToString();
                    Total.Text = (total1 + total2 + total3).ToString();
    

    4

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.