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(C ontainer.D ataItem, "prod_code") %>'></asp:Label> </td>
<td><asp:TextBox id="txtQty" runat="server" size="1" Text='<%#DataBinder.Eval(C ontainer.D ataItem, "prod_qty") %>'></asp:TextBox></td>
<td><asp:Label id="lbl_ProdName" runat="server" Text='<%#DataBinder.Eval(C ontainer.D ataItem, "prod_name") %>'></asp:Label> </td>
<td><asp:Label id="lbl_Price" runat="server" Text='<%#DataBinder.Eval(C ontainer.D ataItem, "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_Pr ice")).Tex t);
prod_qty = Convert.ToDecimal(((TextBo x) e.Item.FindControl("txtQty ")).Text);
prod_total = Convert.ToString(prod_pric e * prod_qty);
return prod_total;
}
Thanks in advance...
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(C
<td><asp:TextBox id="txtQty" runat="server" size="1" Text='<%#DataBinder.Eval(C
<td><asp:Label id="lbl_ProdName" runat="server" Text='<%#DataBinder.Eval(C
<td><asp:Label id="lbl_Price" runat="server" Text='<%#DataBinder.Eval(C
<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)
prod_qty = Convert.ToDecimal(((TextBo
prod_total = Convert.ToString(prod_pric
return prod_total;
}
Thanks in advance...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
providing there are no nulls :)
ASKER
Nice one and thanks very much!