Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XSLT for Beginners

Posted on 2004-04-12
22
Medium Priority
?
609 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]
0
Comment
Question by:oMaverickoVB
  • 12
  • 9
22 Comments
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805257
Your problem is the quotes. Try this:

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

Author Comment

by:oMaverickoVB
ID: 10805283
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?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805349
I see. So you are you trying to insert VB.net code here, in the onclick event of the link?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:oMaverickoVB
ID: 10805369
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
0
 

Author Comment

by:oMaverickoVB
ID: 10805597
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?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805646
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
0
 

Author Comment

by:oMaverickoVB
ID: 10805677
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
0
 

Author Comment

by:oMaverickoVB
ID: 10805710
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?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805752
<input type='button' onclick='textbox1.value=1'></input>
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805755
You can put javascript in there, no problem.
0
 

Author Comment

by:oMaverickoVB
ID: 10805801
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
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805806
Is the product id in the xml file that you are tranforming?
0
 

Author Comment

by:oMaverickoVB
ID: 10805817
The product id is exactly the same as the product name which is being passed.  Is that a problem?
0
 
LVL 3

Accepted Solution

by:
ljfernandez earned 500 total points
ID: 10805975
OK, the solution I'm talking about is something like this, but it will need tweaking. I'm assuming the "." value is the product id.

 <form action="AddToCart.aspx">
<input type="hidden" id="prodID"/>

<xsl:for-each select=".">
<xsl:variable name="prodID"><xsl:value-of select="."/></xsl:variable>

<input type='button' onclick='document.getElementById("prodID").value={$prodID};document.forms[0].submit();' value='Add To Cart'/>
                                    
</xsl:for-each>
</form>

Then in the code behind of AddToCart.aspx, you have to grab the value of request.prodID.
0
 

Author Comment

by:oMaverickoVB
ID: 10805993
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?
0
 

Author Comment

by:oMaverickoVB
ID: 10806099
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
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10806100
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
0
 

Author Comment

by:oMaverickoVB
ID: 10806122
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.
0
 

Author Comment

by:oMaverickoVB
ID: 10806198
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
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10806503
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
0
 

Author Comment

by:oMaverickoVB
ID: 10806525
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question