views:

971

answers:

2

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.

+2  A: 

XSLT can generate pretty much anything you want - but you need to know what you want to generate first.

In ASP.Net I would recommend doing this using the CommandArgument and OnCommand event.

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

Then the single event handler can handle all the button events.

Seeing as I have no idea what your input XML looks like it is very hard to guess how you could generate this in XSLT, but you probably would make good use of Attribute Value Templates, like so:

<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>
samjudson
+1  A: 

Why not use an XmlDataSource with a GridView or Repeater, which ever is more appropriate and use a Template to generate custom buttons bound to the appropriate properties from the Xml element? You can still use XSLT to transform the data (sort, extract subsets, select properties, etc.) if needed.

tvanfosson