[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 965
  • Last Modified:

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:

            <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...
  • 2
1 Solution
hmm ...

why not just do this ...

<%# (Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price")) *Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Quantity"))).ToString()%>
w3digitalAuthor Commented:
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 :)

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now