2011-02-06 134 views
0

我最近決定在我爲了娛樂而構建的電子商務網站上實施稅收,而且我碰到了一個絆腳石。ASP.NET Datalist計算字段

我的實施效果很好,稅收正確應用等等,但是一位好友今天指出,產品頁面上的價格通常顯示含稅。

我在想,與其編輯業務和數據層,我可以在DataList本身上進行更改,但我無法爲我的生活制定出實際的做法。我已經在一些課本一看,有在網上四處搜索,而是因爲我真的不知道我在尋找,我堅持:(

Datalist中:

<asp:DataList ID="list" runat="server" RepeatColumns="2" CssClass="ProductList" RepeatDirection="Horizontal" 
    Width="542px" EnableViewState="False" onitemcommand="list_ItemCommand"> 
<ItemTemplate> 
<div style="width:271px;"> 
<h3 class="ProductName"><a href="<%# Link.ToProduct(Eval("ProductID").ToString()) %>"><%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %></a></h3> 
<a href="<%# Link.ToProduct(Eval("ProductID").ToString()) %>"><img width="100" border="0" src="<%# Link.ToProductImage(Eval("Thumbnail").ToString()) %>" alt='<%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %>' /></a> 
<%# HttpUtility.HtmlEncode(Eval("Description").ToString()) %> 
<p> 
<b>Price:</b> 
<%# Eval("Price", "{0:c}") %> 
</p> 
<p> 
<asp:Button ID="addToCartButton" runat="server" Text="Add to Basket" CommandArgument='<%# Eval("ProductID") %>' CssClass="SmallButtonText" /> 
</p> 
</div> 
</ItemTemplate> 
</asp:DataList> 
後面

代碼:

// Retrieve the list of products in a Sub Category 
    list.DataSource = CatalogAccess.GetProductsInSubCategory(subCategoryId, page, out howManyPages); 
    list.DataBind(); 

例如,如果在DB價格£5,我需要它被顯示在如以上£6,其中包括20%的當前英國增值稅率數據列表

所以: DBPrice * 1.2 = IncVatPrice

我希望這是有道理的!

由於提前, 馬特

回答

0

而不是

<%# Eval("Price", "{0:c}") %> 

使用有點類似

<%# String.Format("{0:c}", (Decimal)Eval("Price")*1.2) %> 

表達式或實現代碼隱藏功能:

protected string IncludeVat(object dataItem) 
{ 
    Decimal price = (Decimal)DataBinder.Eval(dataItem, "Price"); 
    return String.Format("{0:c}", price * 1.2); 
} 

,並呼籲它在你的DataList像

<%# IncludeVat(Container.DataItem) %> 

http://www.pluralsight-training.net/community/blogs/fritz/archive/2005/12/16/17507.aspx