ショッピングカートを表示するListViewがあります。String.Formatを追加して、LineTotalの10進数を通貨文字列として表示するまでは、正常に機能していました。LineTotalでEvalを実行したときに機能していました。
String.Formatを追加すると、問題が発生します。コードビハインドが台無しになります。エラー:入力文字列が正しい形式ではありませんでした。
C#では、テキストラベルの値を取得し、それをコードビハインド全体で使用して、製品の合計値(sum var)を合計するなどの操作を行います。
PriceLabelの価格を通貨として表示したいのですが、合計変数を更新できるように、Listviewデータバインド関数でもラベルの値を使用できるようにする必要があります。
省略形のItemTemplate:
<ItemTemplate>
<asp:Label ID="PriceLabel" runat="server" Text='<%# String.Format("{0:C}", Eval("LineTotal"))%>' ></asp:Label>
</ItemTemplate>
コードビハインド:
protected void ProductListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
//update subtotal by finding the label string value
Label lbl = (Label)e.Item.FindControl("PriceLabel") as Label;
decimal sublinetotal = Convert.ToDecimal(lbl.Text);// <---- Input error here
//update the sum var to show the total price on the page
sum += sublinetotal;
}
}