views:

371

answers:

1

This should be simple enough, and I blame this primarily on my inexperience working with DataLists, Repeaters, etc.

I have a DataList control which I am using to display the items in a shopping cart. Everything is going along fine with my binding of the different controls using Eval("ColumnName") until I get to the price column.

My goal is to render it like so:

Total Item Cost

$20.00 ($5.00 each)

I am trying to accomplish this with two ASP Label controls inside the table cell:

<asp:Label ID="lblTotalItemCost" runat="server" Text='<%# Eval("TotalItemCost") %>'>
</asp:Label>
<br />
<asp:Label ID="lblPrice" runat="server" Text='(<%# Eval("Price")%> each)' >
</asp:Label>

Unfortunately it is rendering out the column as:

Total Item Cost

$20.00 (<%# Eval("Price")%> each)

I've tried a couple other methods, but they end up giving me "server tag not well formed" errors. I am pretty sure this should be simple but I'm stumped at the moment.

+3  A: 

Try this instead:

Text='<%#"(" + Eval("Price").ToString() + " each)"%>'
Ahmad Mageed
Option Strict On prohibits operands of type Object for operator '+'. That's closer than what I had before (thanks to the double quotes, which I had screwed up).
TheTXI
I just added .ToString to the Eval("Price") and now it works. Thanks a lot Ahmad.
TheTXI
I thought ToString is a method call so would be ToString(), since you are dealing with monetary ammounts you can add a string format say ToString('0.##') to get the formatting.
Dave Anderson
Dave you're right. In C# calling it as ToString would cause the compiler to complain. However, this is not the case in VB.NET where using ToString and ToString() are both acceptable, although probably confusing to C# devs at first. In fact, here's a more complete answer: http://stackoverflow.com/questions/390648/tostring-vs-tostring-in-vb-net
Ahmad Mageed
Good catch on that Ahmad (you too Dave). I'll edit the answer again to reflect the more friendly version.
TheTXI