How do I multiply 2 columns in a Repeater?

Posted on 2005-04-25
Last Modified: 2008-02-01
I have a shopping script using a repeater to display items and prices.

On a row by row basis, I need to multiply the price by the quantity to get a sub total

The repeater is called "rpBasket". I figured I'd use a method "rowTotal()" and grab (and convert) the qty textbox and label price multiply them and return it. Here's the item template for the repeater:

            <td><asp:Label id="lbl_ProdCode" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "prod_code") %>'></asp:Label> </td>
            <td><asp:TextBox id="txtQty" runat="server" size="1" Text='<%#DataBinder.Eval(Container.DataItem, "prod_qty") %>'></asp:TextBox></td>
            <td><asp:Label id="lbl_ProdName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "prod_name") %>'></asp:Label> </td>
            <td><asp:Label id="lbl_Price" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "prod_price") %>'></asp:Label></td>

This worked on a simple test (where I created 2 literals and added them) so I guess I'm just dumb when it comes to getting the values out of the repeater. Here's the method which is naff and doesn't work but you can see what I am trying to do...

            public string rowTotal(object source, RepeaterItemEventArgs e)
                  decimal prod_price;
                  decimal prod_qty;
                  string prod_total;
                  prod_price = Convert.ToDecimal(((Label) e.Item.FindControl("lbl_Price")).Text);
                  prod_qty = Convert.ToDecimal(((TextBox) e.Item.FindControl("txtQty")).Text);
                  prod_total = Convert.ToString(prod_price * prod_qty);
                  return prod_total;

Thanks in advance...
Question by:w3digital
    LVL 37

    Accepted Solution

    hmm ...

    why not just do this ...

    <%# (Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price")) *Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Quantity"))).ToString()%>

    Author Comment

    Thanks. Now why didn't I think of that... Ah yes, I'm a newbie. That'll be it!

    Nice one and thanks very much!
    LVL 37

    Expert Comment

    providing there are no nulls :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now