ASP.NET 将 gridview 单元格值传递给 javascript

发布于 2024-12-18 08:39:44 字数 449 浏览 2 评论 0原文

我有一个网格视图,其中包含许多产品的详细信息。我正在尝试使用 simplecart (http://simplecartjs.com/) 将购物车功能添加到我的页面,我被困在一件事上。为了将商品添加到购物车,您必须使用以下链接,我将其添加到 gridview 中的列中:

<a href="javascript:;" onclick="simpleCart.add( 'name=Name' , 'price=123' , 'quantity=1' );">Add To Cart</a>

我想将 ProductName 列和 ProductPrice 列中的值传递给上面每行的名称和价格,但是不知道该怎么做。我对 html/asp 等完全陌生,所以如果这是一个简单的问题,请原谅我。

I have a gridview which contains the details of a number of products. I'm trying to use simplecart (http://simplecartjs.com/) to add shopping cart functionality to my page, and am stuck on one thing. In order to add an item to the cart you have to use the following link, which I added to a column in the gridview:

<a href="javascript:;" onclick="simpleCart.add( 'name=Name' , 'price=123' , 'quantity=1' );">Add To Cart</a>

I want to pass the values in the ProductName column and ProductPrice column to name and price above for each row, but have no idea how to go about it. I am completely new to html/asp etc. so please forgive me if it is a simple question.

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

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

发布评论

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

评论(1

幽蝶幻影 2024-12-25 08:39:44

假设产品名称和产品价格位于您要绑定的数据源中

onclick="simpleCart.add( 'name=<%# Eval("ProductName") %>' , 'price=<%# Eval("ProductPrice") %>' , 'quantity=1' );">Add To Cart</a>

(您也可以使用 Bind 而不是 Eval - Bind 是两种方式绑定,Eval 是一种方式。在您的情况下,Eval 没问题)。

如果这些值不直接位于数据源中,即需要组合两个字段或在显示之前需要一些复杂的格式,那么您可以使用 RowDataBound 事件将它们注入到您的锚标记中。锚标记需要是 HTMLControl 才能工作,即 runat="server" - 请参阅 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx 了解详细

信息担心对于简单的问题,我只能提供简单的答案!

Assuming the product name and product price are in the data source you are binding to

onclick="simpleCart.add( 'name=<%# Eval("ProductName") %>' , 'price=<%# Eval("ProductPrice") %>' , 'quantity=1' );">Add To Cart</a>

(You could also use Bind rather than Eval - Bind is two way binding Eval is one way. In your case Eval is fine).

If the values aren't directly in the data source i.e need combining from two fields or need some complex formatting before display then you can use the RowDataBound event to squirt them into your anchor tag. The anchor tag will need to be a HTMLControl for this to work i.e. runat="server" - see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx for details

Don't worry about simple questions, i can only provide simple answers!

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