Link to home
Start Free TrialLog in
Avatar of w3digital
w3digital

asked on

How do I multiply 2 columns in a Repeater?

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:

<ItemTemplate>
      <tr>
            <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>
            <td><%#rowTotal()%></td>
      </tr>
</ItemTemplate>

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...
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of w3digital
w3digital

ASKER

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!
providing there are no nulls :)