Copied to clipboard

Flag this post as spam?

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


  • philw 99 posts 434 karma points
    May 03, 2013 @ 00:34
    philw
    0

    Passing variable from C# to a macro

    I'm trying to pass a value from a C# Master Page (template) to an Umbraco macro. The value is a "mode" which the user can be in - actually one of three sports which they user can choose. The value controls what content is shown, and also what menu items are visible. I need to pass the value to the menu-generation XSLT so it can use it to selectively show/ hide pages.

    I back the value off in session and also in a cookie, so there lots of ways I can pull it.

    I can think of/ have tried the following approaches:

    1. Pass the value as a straight macro parameter (from the Master Page c#). This doesn't work, it's not parsed at all. I can't get a value into a Macro from c# this way. There are other threads here which suggest that is correct.

    2. Pass it in the cookie. That works, in that I can read the value in the XSLT.

    3. Pass it in session. That also works. 

    The problem is that the value always seems to be "one step behind" in the life cycle, when it changes. So when I change it, the first time I post back nothing happens. The next postback, and it works perfectly. It's almost like the value isn't changing until after the XSLT is run, after the menu markup is generated.

    A slightl complication is that I pass the sport name as a url which is rewritten - I convert a url ending in (say) /cricket to be ?sport=cricket, using umbraco's built-in url rewriting (not asp.net's).

    That sounds like a page lifecycle issue, like say the XSLT is running at Page_Load and my detecting the changed value is later. I put in an OnInit() handler which specifically parses the ?sport value and sets the value in session though, so I can't see how that's happening.

    I have caching on the XSLT macro turned off.

    Does anyone have any idea on what's happening there, or know a way around it?

     

    The only thing I can think of really is to dump the XSLT and write a c# User Control to generate my own menu. I can definitely read session after PreInit there, so that should work. If I do this I'd need to get to the Umbraco node XML from c# - does anyone know how to get that?

    thanks in advance...

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 03, 2013 @ 00:46
    Chriztian Steinmeier
    0

    Hi philw,

    Take a look at the Advanced Macro Parameter Syntax - you can pass QueryString-, Cookie- and Session values to the macro from the Master template using this syntax.

    If you're manipulating some values during Page_Load (I don't know much about how this works in .NET, though) - you could use a ContextKey item - which you can fetch from within the XSLT with the ContextKey() method in the library...

    /Chriztian 

  • philw 99 posts 434 karma points
    May 03, 2013 @ 10:34
    philw
    0

    I'm getting the value passed through session, but I have the problem which is mentioned here (third post down), which is that the value is always "one step behind" what it should be.

    I think this is some life cycle/ Umbraco thing, or possibly the XSLT being cached, although I turned that off.

    Possible ways forward  I can think of:

    1. Figure out why the XSLT variable value is "one step behind" and fix it. Server.transfer approaches aren't acceptable fixes I think.
    2. Recode the menu as a user control. This would be easy if I can get the Umbraco xml in c# somehow.
    I'll google for that "one step behind" thing a bit more, as it must be a common issue I think.

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 03, 2013 @ 10:44
    Chriztian Steinmeier
    100

    Am I right in the following:

    - Original request is /somepage/cricket

    - Rewrite makes request for /somepage?sport=cricket

    ?

    If that's the case, you should be able to use Advanced Macro Parameter Syntax to get the QueryString value:

    <umbraco:Macro alias="..." sport="[@sport]" runat="server" />

    Have you tried this?

    Alternatively, you could also use the umbraco.library:RequestQueryString() extension from within XSLT:

    <xsl:variable name="sport" select="umbraco.library:RequestQueryString('sport')" />

     

    /Chriztian

  • philw 99 posts 434 karma points
    May 03, 2013 @ 10:50
    philw
    0

    You're right - I'll try those today and post how it goes. Thanks very much for your help!

  • philw 99 posts 434 karma points
    May 03, 2013 @ 19:34
    philw
    0

    Success! I tried it several ways. I could not get your second version to work, but the first works perfectly, and it has the advantage that if I turn on caching, as Umbraco "knows" about the parameter, it will refresh when that changes.

    So, that works perfectly, thanks. The Macro code is as your #1 example, and the XSLT is:

    <xsl:variable name="sport" select="/macro/sport" />
    <xsl:variable name="contentField" select="concat('leftColumnText-',$sport)" />

    ...because the field name I'm checking is called "leftColumnText-sport-name". Works perfectly, changes instantly (so the user changes the sport, and the menu changes immediately.

    Perfect... just one more issue to nail, which I hope I'll get without assistance. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft