0

I am tryiing to create an "add to cart" button for each item that is displayed by an XSLT file. The button must be run at server (VB) and I need to pass parameters into the onlick, so that the requested item is added to the cart. Is this possible, and if so, how should I go about it?

When I try

<asp:Button id="Button123"
   Text="Add to Cart"
   CommandName="AddToCart"
   CommandArgument="123"
   OnCommand="CommandBtn_Click" 
   runat="server"/>

I get "'asp' is an undeclared namespace"

I've also tried

<asp>
   <xsl:attribute name="Button">id="BtnAddToCart"</xsl:attribute>
   <xsl:attribute name="text">Add to cart</xsl:attribute>
   <xsl:attribute name="CommandName">AddToCart</xsl:attribute>
   <xsl:attribute name="CommandArgument">123</xsl:attribute>
   <xsl:attribute name="Command">CommandBtn_Click</xsl:attribute>
   <xsl:attribute name="runat">server"</xsl:attribute>
</asp>

Which doesn't give any errors, but doesn't do anything at all

I need to use XSLT directly for displaying my products, as it is for an assignment, although what I am trying to do here is beyond the scope of the assignment.

4

2 回答 2

2

XSLT 几乎可以生成任何你想要的东西——但你需要先知道你想要生成什么。

在 ASP.Net 中,我建议使用 CommandArgument 和 OnCommand 事件来执行此操作。

<asp:Button id="Button123"
       Text="Add to Cart"
       CommandName="AddToCart"
       CommandArgument="123"
       OnCommand="CommandBtn_Click" 
       runat="server"/>

然后单个事件处理程序可以处理所有按钮事件。

看到我不知道您的输入 XML 是什么样子,很难猜测您如何在 XSLT 中生成它,但您可能会充分利用属性值模板,如下所示:

<xsl:for-each select="Item">
  ...
  <asp:Button id="Button{@Id}"
       Text="Add To Cart"
       CommandName="AddToCart"
       CommandArgument="{@Id}"
       OnCommand="CommandBtn_Click" 
       runat="server"/>
</xsl:foreach>
于 2008-10-12T11:49:16.443 回答
1

为什么不将 XmlDataSource 与 GridView 或 Repeater 一起使用,哪个更合适,并使用模板生成绑定到 Xml 元素的适当属性的自定义按钮?如果需要,您仍然可以使用 XSLT 来转换数据(排序、提取子集、选择属性等)。

于 2008-10-12T17:32:18.280 回答