ASP.NET Datalist 计算字段

发布于 2024-10-16 03:51:50 字数 1682 浏览 5 评论 0原文

我最近决定对我为乐趣而建立的电子商务网站征税,但我遇到了绊脚石。

我的实施效果很好,税收应用正确等,但是今天一位朋友向我指出,产品页面上的价格通常显示含税。

我在想,我可以在 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();

例如,如果数据库中的价格是 5 英镑,我需要将其在上面的数据列表中显示为 6 英镑,其中包括当前的英国 20% 增值税率,

因此: DBPrice * 1.2 = IncVatPrice

我希望这是有道理的!

提前致谢, 马特

I recently decided to implement tax on an ecommerce website I've built for fun more than anything and I've hit a stumbling block.

My implementation works well, tax is applied correctly etc, however a buddy pointed out to me today that prices on product pages are normally displayed inc tax.

I was thinking, rather than editing the Business and Data tier, I could change this on the DataList itself but I can't for the life of me work out how I'd actually do that. I've had a look in some text books and had a search around online but because I don't actually know what I'm looking for, I'm stuck :(.

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>

Code behind:

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

E.g. if the price in the DB is £5, I'd need it to be displayed in the datalist above as £6 which includes the current UK VAT rate of 20%.

So:
DBPrice * 1.2 = IncVatPrice

I hope this makes sense!

Thanks in advance,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜声 2024-10-23 03:51:50

而不是

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

使用有点类似于的表达式

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

Or Implement a function in codebehind:

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

Instead of

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

use an expression somewhat similar to

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

Or implement a function in codebehind:

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

and call it in your DataList like that

<%# IncludeVat(Container.DataItem) %>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文