Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • trfletch 598 posts 604 karma points
    Nov 16, 2009 @ 17:15
    trfletch
    0

    Moving ad rotator into XSLT code

    Hi, I have an Umbraco V4 website that currently has a top banner that is shown using the following code in the master template:

     <cc2:CAdRotator id="CAdRotator1" runat="server" 
          AdvertisementFile="/App_Data/FlashTop.xml" ></cc2:CAdRotator>

    I want to have the option of displaying an image instead of this flashbanner on certain pages so I created the following XSLT but I get an XSLT error when I try to put it on my page. I assume I need to wrap the code above in something to put it in my XSLT but I don't know what, can someone please help. The whole XSLT I have so far is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxml="urn:schemas-microsoft-com:xslt"
     xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">






    <xsl:choose>
    <xsl:when test="$currentPage/data [@alias = 'topbannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='topbannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='topbannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='topbannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:when>
    <xsl:otherwise>
    <cc2:CAdRotator id="CAdRotator1" runat="server"
          AdvertisementFile="/App_Data/FlashTop.xml" ></cc2:CAdRotator>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
  • dandrayne 1138 posts 2262 karma points
    Nov 16, 2009 @ 17:36
    dandrayne
    0

    Perhaps it's a namespace issue?

    You may need to add something like

    xmlns:cc2="urn:make-something-up-com:xslt"

     

    Dan

  • trfletch 598 posts 604 karma points
    Nov 16, 2009 @ 18:38
    trfletch
    0

    Thanks for the response Dan, it no longer shows an XSLT error and it does show an alternative image if I select one but it doesn't show the flash banner on pages where I haven't selected an alternative image, I have check the source code and it is just outputing the code from the XSLT as opposed to executing it as such, any ideas why it is doing this and what I can do to resolve it? This is the output source from my XSLT:

    <cc2:CAdRotator id="CAdRotator1" runat="server" AdvertisementFile="/App_Data/FlashTop.xml" xmlns:cc2="urn:CAdRotator:xslt" />
    

    And this is what gets outputted when I don't use the XSLT and just have the CAdRotator directly in the page:

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
    codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="620" height="220">
    <param name="movie" value=/flash/testimonials1.swf>
    <param name="quality" value="high">
    <embed src=/flash/testimonials1.swf quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="620" height="220"></embed>
    </object>

    Anyone got any ideas?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 16, 2009 @ 21:59
    Lee Kelleher
    0

    The "<cc2:CAdRotator />" tag is a .NET control - you wont be able to execute it in the XSLT, (as it is called too late in the ASP.NET Page Lifecycle).

    What you need to do is use the same logic in a .NET user-control.  Where you would hide the "<cc2:CAdRotator Visible="false" />" - then check if the property ("topbannerimage" from the current page), has a value ... if so, then use an <asp:Image /> control to display the image ... otherwise set the "CAdRotator" to visible.

    In a nutshell, you're going down the wrong route trying to do this in XSLT.

    Cheers, Lee.

  • trfletch 598 posts 604 karma points
    Nov 17, 2009 @ 10:35
    trfletch
    0

    Thanks for the response Lee, hmmm that's not good news for me because the reason I was trying to use XSLT is because I do not know asp.net very well.

  • dandrayne 1138 posts 2262 karma points
    Nov 17, 2009 @ 11:38
    dandrayne
    0

    Depending on the complexity of the ad rotator, this could potentially be managed in umbraco as well?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2009 @ 11:58
    Lee Kelleher
    1

    Tony,

    This isn't tested in any form, but here's an example .NET user-control you can try:

    <%@ Control Language="C#" %>
    <%@ Register TagPrefix="cc2" TagName="CAdRotator" Src="~/CAdRotator.ascx" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Int32 mediaId = 0;
    
            // get the 'topbannerimage' media id
            String topBannerNodeId = umbraco.library.GetItem("topbannerimage");
    
            // try to parse the media id
            if (Int32.TryParse(topBannerNodeId, out mediaId))
            {
                // get the media item
                umbraco.cms.businesslogic.media.Media topBanner = new umbraco.cms.businesslogic.media.Media(mediaId);
    
                // check if that media item is not null
                if (topBanner != null)
                {
                    // set the properties of the image
                    this.Image1.ImageUrl = topBanner.getProperty("umbracoFile").Value.ToString();
                    this.Image1.Width = new Unit(topBanner.getProperty("umbracoWidth").Value.ToString());
                    this.Image1.Height = new Unit(topBanner.getProperty("umbracoHeight").Value.ToString());
    
                    // set the image to visible
                    this.Image1.Visible = true;
    
                    // hide the ad rotator
                    this.CAdRotator1.Visible = false;
                }
                else
                {
                    // hide the image
                    this.Image1.Visible = false;
    
                    // show the ad rotator
                    this.CAdRotator1.Visible = true;
                }
            }
        }
    </script>
    
    <asp:Image runat="server" ID="Image1" Visible="false" />
    
    <cc2:CAdRotator runat="server" ID="CAdRotator1" AdvertisementFile="/App_Data/FlashTop.xml" Visible="false" />
    

    It might look overly complicated ... but the logic is similar to what you are trying to achieve in the XSLT.

    Obviously, you'll need to change the reference to the CAdRotator control.

    Good luck!

    Cheers, Lee.

  • trfletch 598 posts 604 karma points
    Nov 17, 2009 @ 15:57
    trfletch
    0

    Hi Lee,

    Thanks for this, I spent 2 hours today trying to work out just how to display an image from an Umbraco property using asp.net but the closest I got was displaying the node ID! As I said before my asp.net stills are none existant. I now have the following code taken from what you wrote above with a couple of small modifications but I cannot seem to get it to display the adrotator, if I change the visibilty of the ad rotator to true then it will display (all the time regardless of whether there is a topbannerimage) but it seems that for some reason the else statement that tells the adrotator to change visibility to true is not being picked up, any ideas why this would be?

    <%

    @ Control Language="C#" %>

    <%

    @ Register Assembly="CAdRotator" Namespace="CAdRotator" TagPrefix="cc2" %>

    <

     

    script runat="server">

     

    protected void Page_Load(object sender, EventArgs e)

    {

     

    Int32 mediaId = 0;

     

     

    // get the 'topbannerimage' media id

     

    String topBannerNodeId = umbraco.library.GetItem("topbannerimage");

     

     

    // try to parse the media id

     

    if (Int32.TryParse(topBannerNodeId, out mediaId))

    {

     

    // get the media item

    umbraco.cms.businesslogic.media.

    Media topBanner = new umbraco.cms.businesslogic.media.Media(mediaId);

     

     

    // check if that media item is not null

     

    if (topBanner != null)

    {

     

    // set the properties of the image

     

    this.Image1.ImageUrl = topBanner.getProperty("umbracoFile").Value.ToString();

     

    this.Image1.Width = new Unit(topBanner.getProperty("umbracoWidth").Value.ToString());

     

    this.Image1.Height = new Unit(topBanner.getProperty("umbracoHeight").Value.ToString());

     

     

    // set the image to visible

     

    this.Image1.Visible = true;

     

    }

     

    else

    {

     

    // show the ad rotator

     

    this.CAdRotator1.Visible = true;

    }

    }

    }

     

    </script>

     

    <asp:Image runat="server" ID="Image1" Visible="false" />

     

    <cc2:CAdRotator runat="server" ID="CAdRotator1" AdvertisementFile="/App_Data/FlashTop.xml" Visible="false" />

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2009 @ 16:17
    Lee Kelleher
    0

    Hmmm... that sounds like strange behaviour of the CAdRotator control.

    I suggest that you wrap an <asp:Placeholder> around the CAdRotator tag and set the Visibility of that instead.

    Let me know if you need further explanation.

  • trfletch 598 posts 604 karma points
    Nov 17, 2009 @ 16:39
    trfletch
    0

    Hi Lee,

    Thanks for the quick response, I have tried what you said and now have the following code but it is still behaving exactly the same, it's almost as if it's not running the else command:

    <%

    @ Control Language="C#" %>

    <%

    @ Register Assembly="CAdRotator" Namespace="CAdRotator" TagPrefix="cc2" %>

    <

     

    script runat="server">

     

    protected void Page_Load(object sender, EventArgs e)

    {

     

    Int32 mediaId = 0;

     

     

    // get the 'topbannerimage' media id

     

    String topBannerNodeId = umbraco.library.GetItem("topbannerimage");

     

     

    // try to parse the media id

     

    if (Int32.TryParse(topBannerNodeId, out mediaId))

    {

     

    // get the media item

    umbraco.cms.businesslogic.media.

    Media topBanner = new umbraco.cms.businesslogic.media.Media(mediaId);

     

     

    // check if that media item is not null

     

    if (topBanner != null)

    {

     

    // set the properties of the image

     

    this.Image1.ImageUrl = topBanner.getProperty("umbracoFile").Value.ToString();

     

    this.Image1.Width = new Unit(topBanner.getProperty("umbracoWidth").Value.ToString());

     

    this.Image1.Height = new Unit(topBanner.getProperty("umbracoHeight").Value.ToString());

     

     

    // set the image to visible

     

    this.Image1.Visible = true;

     

     

    // hide the ad rotator

     

    //this.CAdRotator1.Visible = false;

    }

     

    else

    {

     

    // show the ad rotator

     

    this.adplaceholder.Visible = true;

    }

    }

    }

     

    </script>

     

    <asp:Image runat="server" ID="Image1" Visible="false" />

     

    <asp:PlaceHolder runat="server" ID="adplaceholder" Visible="false" >

     

    <cc2:CAdRotator runat="server" ID="CAdRotator1" AdvertisementFile="/App_Data/FlashTop.xml"/>

     

    </asp:PlaceHolder>

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2009 @ 16:47
    Lee Kelleher
    0

    Hi Tony,

    I think a little debugging is needed. Let's see what's in the "mediaId" ...

    After the "if (Int32.TryParse(topBannerNodeId, out mediaId))" ... add a "Response.Write( mediaId.ToString() );"

    Depending on what the value is, we may need to add another condition ... like "if (mediaId > 0)" ... as I have a sneaky feeling that "new Media( mediaId ) isn't returning null.  But it's difficult to tell without further debugging.

    Cheers, Lee.

  • trfletch 598 posts 604 karma points
    Nov 17, 2009 @ 17:04
    trfletch
    0

    Thanks again for the quick response, I have added that line in underneath the line you said "if (int32..........." and on the page that does have a topbannerimage it is displaying the ID of that image but on a page that doesn't have a topbannerimage there is the following error page, have I put it in the wrong place or should I have removed something else when adding it?:

     

    Server Error in '/' Application.

    No node exists with id '0'

     

     

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: No node exists with id '0'

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2009 @ 17:29
    Lee Kelleher
    0

    Try changing the condition from:

    if (Int32.TryParse(topBannerNodeId, out mediaId))

    to:

    if ( (topBannerNodeId != "0") && (Int32.TryParse(topBannerNodeId, out mediaId)) )

    See if that works?

  • trfletch 598 posts 604 karma points
    Nov 17, 2009 @ 17:52
    trfletch
    1

    Hi Lee,

    I tried what you said above but it still didn't work, I had the same issue, showed the topbannerimage if there was one but showed nothing on other pages, I have managed to get it to work with the following code which I am happy to leave it like this as long as you can't see any issues with doing it this way (as I said before not really up on my asp.net so don't know if this is best practice or not). Not sure if this helps explain what the problem was with the original code? As you can see I have changed the visibility of the adrotator to true, then added into the if statement to make its visibilty false if topbanner isn't null, I then completely removed the Else statement because I don't seem to need it. Let me know what you think:

    <%@ Control Language="C#" %>
    <%@ Register Assembly="CAdRotator" Namespace="CAdRotator" TagPrefix="cc2" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Int32 mediaId = 0;
           
            // get the 'topbannerimage' media id
            String topBannerNodeId = umbraco.library.GetItem("topbannerimage");
          
            // try to parse the media id
            if (Int32.TryParse(topBannerNodeId, out mediaId))
          
            {
                // get the media item
                umbraco.cms.businesslogic.media.Media topBanner = new umbraco.cms.businesslogic.media.Media(mediaId);
               
                // check if that media item is not null
                if (topBanner != null)
                {
                    // set the properties of the image
                    this.Image1.ImageUrl = topBanner.getProperty("umbracoFile").Value.ToString();
                    this.Image1.Width = new Unit(topBanner.getProperty("umbracoWidth").Value.ToString());
                    this.Image1.Height = new Unit(topBanner.getProperty("umbracoHeight").Value.ToString());
                   
                    // set the image to visible
                    this.Image1.Visible = true;
                   
                    // hide the ad rotator
                    this.CAdRotator1.Visible = false;
                   
                   
                }
              
            }
        }
        </script>
        <asp:Image runat="server" ID="Image1" Visible="false" />
        <cc2:CAdRotator runat="server" ID="CAdRotator1" AdvertisementFile="/App_Data/FlashTop.xml" Visible="true"/>
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 17, 2009 @ 20:24
    Lee Kelleher
    0

    Hi Tony, if it works - that's great! Glad it's working for you now.

    (remember to mark the solution - or at least spread some karma / thumbs-up!) :-D

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft