We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

XSLT for Beginners

Medium Priority
639 Views
Last Modified: 2013-11-18
Hey guys,

I'm in desperate need for help!  Isn't everyone that post though :)

Ok.  I'm really new to XSLT so I'm guessing the problem I'm having is easily overcome... I hope cos I need to get this thing finished in about 5 hours.  Everything is working on my webpage except for one thing...  I need to add a button in the XSLT.  The code supplied works fine except for the button section.  It displays all the products, descriptions,etc...  What I need as well, is to have a button for each product that has the following code attached to it

Context.Items("ProductID") = @name
                             (the name of the current product name)
Server.Transfer("AddToCart.aspx")

So it calls the addtocart page and the code in that adds the product to the shopping cart.

Ok, in the code below, I've tried heaps of things to get this working but I just can't figure it out.

Guys, any help here ASAP would be greatly appreciated!  I'm a the business end of the rope at the moment so I'm really struggling. :(

Look forward to a solution.
oMavericko

[code]<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version= "1.0">
      <xsl:template match="/">
            
            <xsl:for-each select="/productlist/product">
                  <TABLE width="75%" align="center" border="1">
                        <tr>
                              <td>Name: </td>
                              <td><xsl:apply-templates select="name"/></td>
                              <td RowSpan = "4" width= "0"> <xsl:apply-templates select="image"/></td>
                        </tr>
                        <tr>
                              <td>Code: </td>
                              <td><xsl:apply-templates select="code"/>t</td>
                        </tr>
                        <tr>
                              <td>Category: </td>
                              <td><xsl:apply-templates select="category"/></td>
                        </tr>
                        <tr>
                              <td>Price: </td>
                              <td><xsl:apply-templates select="price"/></td>
                        </tr>
                        <tr>
                              <td>Description: </td>
                              <td><xsl:apply-templates select="description"/></td>
                              <td>
                                    <input type="button" onclick="Context.Items("ProductID") = prodid;Server.Transfer("AddToCart.aspx")" value="Add To Cart"/>
                              </td>
                        </tr>
                  </TABLE>
                  <p/>
            </xsl:for-each>
      </xsl:template>
      
      <xsl:template match="name">
            <b><xsl:value-of select="."/> </b>
      </xsl:template>
                  
      <xsl:template match="code">
            <xsl:value-of select="."/>
      </xsl:template>
      
      <xsl:template match="price">
            $<xsl:value-of select="."/>.00
      </xsl:template>
      
      <xsl:template match="category">
            <xsl:value-of select="."/>
      </xsl:template>
      
      <xsl:template match="description">
            <xsl:value-of select="."/>
      </xsl:template>  
                                  
      <xsl:template match="image">
            <img>
                  <xsl:attribute name="src">
                        <xsl:value-of select="." />
                  </xsl:attribute>
            </img>      
         </xsl:template>
         
</xsl:stylesheet>[/code]
Comment
Watch Question

Your problem is the quotes. Try this:

<input type='button' onclick='Context.Items("ProductID") = prodid;Server.Transfer("AddToCart.aspx")' value='Add To Cart'/>

Author

Commented:
mmm... I tried that.  It didn't work.  I can't seem to get any code to work here.  Is there a bit of test code I could put in there to see if I can get that working.  I'm a VB Programmer, so I'm used to using MSGBOX("test") to see if anything works.  can I do that here?
I see. So you are you trying to insert VB.net code here, in the onclick event of the link?

Author

Commented:
yep, is that possible?  I basically need to pass the current product to a webpage.  Is this possible???  Can you show me a simple how to do this?

cheers

Author

Commented:
sorry, I don't understand. I thought this was in the XML area?  What do I need to do?  Is there a link I need to follow or anything?
Is the product id in the xml file that you are tranforming?

If so, you could create a hidden form field on the page, in the onclick event have a javascript which passes the value to the hidden field and submits the form to AddToCart.aspx. This file's code behind would then read it and perform the needed action.

Lisa

Author

Commented:
The product id is the product name in this case.  So it's already in the XML file.  Can you show what code I need to have using the context items and server.transfer code?  I've been told I need to do it this way.

Is it possible for you to show me on my example above?  

Thanks in advance

Author

Commented:
Ok.  No matter what code I put in the onclick... I'm getting an error.  I put this line of code in just to see if this is where my problem lies...

           <input type='button' onclick='textbox1.text=1'></input>

All I got from this is an error.... not cool.  any reason why?
<input type='button' onclick='textbox1.value=1'></input>
You can put javascript in there, no problem.

Author

Commented:
ok... I've worked that part out.  I'm using VS.NET and I was using the textbox from the webforms and not the HTML toolbox.  Ok.  So I've worked that little bit out.  

But I'm still lost about how I use those 2 lines of code I need.  One to set that variable and the other to load the other page....

?? Me lost
Is the product id in the xml file that you are tranforming?

Author

Commented:
The product id is exactly the same as the product name which is being passed.  Is that a problem?
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I'm lost dude.  Where do I put that code in?  I'm really new at this.  Is it a simple case of adding that section in somewhere to the code above?

Author

Commented:
Is it possible for you to add the changes to my code above?  I just need to get this part working and then I can hand it in.  Everything else is working.  The AddToCart page uses the following lines of code:-

Dim prodid as string = Ctype(context.items("ProdId"),String)

and then used the label to get the ProdID:-

lblProductID.Text = ProdID

Thanks
IMO I think your question is too complex for 125pts. The first part goes in the xsl and the second part goes in the code behind for the AddToCart.aspx file.

Lisa

Author

Commented:
sorry, I don't have any more points.  These are my last points available.  I didn't think it was going to be this much of a problem for you.

Sorry about this then.

Author

Commented:
Well, I'm still stuck in the same position I was at the start about 3 hours ago.  Thank you for helping anyway.  I honestly don't understand where that code goes but thank you anyway.  I just gotta stick with it and see what happens.  Sorry, I don't have anymore points to give you.


oMavericko
oMavericko,

Hey no problem, I was just saying that it seems the problem is a bit more complex than it seems on the surface. I can't add my code to your code because of the complexities of combining the two technologies that you are trying to use here. As I understand it, the .Net code is executed on the server side before the XSL transform takes place, which means you can't easily put the .net stuff in the html like you can with a regular .aspx page.  Sorry I couldn't be of more help to you. I feel bad taking your points if my answer doesn't help!

Best of luck with your problem,

Lisa

Author

Commented:
hey, its ok.  I took a punt... it didn't pay off but I guess it kinda showed which way I shouldn't be heading :/

I just find it hard that I can't use those 2 lines of code in the XSL file...  I simply don't have enough knowledge of it to get it done so I'm stuffed now.




thanks anyway
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.