Copied to clipboard

Flag this post as spam?

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


  • Neil Gibbons 144 posts 61 karma points
    Feb 24, 2010 @ 18:16
    Neil Gibbons
    0

    Determine PostBack from within XSLT macro

    Hi,

    Is it possible to determine if the IsPostBack of a page is true from within a macro?

    As per my earlier post, http://our.umbraco.org/forum/developers/xslt/7403-XSLT-problem, I have a macro that writes a WebTrends tracking image tag based on url, path and other properties - but I need it to "know" if there's been a postback, as I have to track a "conversion step", which is essentially someone submitting a form, (which is an additional user control macro on the same page).

    At the moment I have some quick mxml:script stuff to test an approach, with the folloeing:

       public string HasBeenSubmitted()
        {
            string res = "";
            Page page = HttpContext.Current.Handler as Page;
            if (page != null) {
                res = page.IsValid.ToString();
            }
            return res;
        }

    This doesn't seem to work as expected though.

    Perhaps if my form user control added something into HttpContext.Items when its handled the postback - my WebTrends macro could then grab this?

    Has anyone ever used HttpContext.Items to pass data between macros?

     

    Neil

     

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 24, 2010 @ 18:29
    Lee Kelleher
    0

    Hi Neil,

    Hmmm... sounds like it might have been easier to write this a .NET user-control macro, rather than muck around with XSLT - but anyway ...

    You could try checking the value of the "REQUEST_METHOD" ServerVariable? Make assumptions like if it's "GET", then it's not IsPostBack, and if it's "POST" then it IsPostBack?

    umbraco.library:RequestServerVariables('REQUEST_METHOD')

     

    Also, I have used HttpContext.Items to pass data between various components - for example, pulling Member data out in the <head> (to display the member's name in the <title>) and then use it again (in a different macro) to display the full member data/details.

    I did write a wrapper XsltExtension around it though, so re-grab the data if it didn't exist in the HttpContext.Item (for various reasons)

    Cheers, Lee.

  • Neil Gibbons 144 posts 61 karma points
    Feb 25, 2010 @ 17:08
    Neil Gibbons
    0

    Lee,

    Do you know if there's any "order" to macros running?

    The reason I ask is that I have usercontrol thats assigning to HttpContext.Current.Items in its Page_load, and then my XSLT macro trying retrieve the value lower down - but its not finding it.

    Could the XSLT macro have been executed before the usercontrol? The usercontrl macro if "higher" in the template?

    Cheers

     

    Neil

     

  • Chris Gaskell 59 posts 142 karma points
    Feb 25, 2010 @ 17:30
    Chris Gaskell
    0

    Neil

    Form what I've found the macros run in the order they are in the templates.

    But yeah - why not use a .NET usercontrol - you constrained by the enviroment Neil?

    @Lee Kelleher's solution is pretty sweet though - I wouldnt have thought of that.

    Chris.

  • Neil Gibbons 144 posts 61 karma points
    Feb 25, 2010 @ 17:35
    Neil Gibbons
    0

    Yeah, problem is we have validation happening on the server side - checking for POST isn't enough - I need to know its valid.

    No constraint, its just that its all written and working ace in XSLT - just this last little bit!

     

    Neil

  • Chris Gaskell 59 posts 142 karma points
    Feb 25, 2010 @ 17:40
    Chris Gaskell
    0

    Neil,

    What's happening with your mxml:script? Doesn't it return the expected value?

    Chris.

  • Neil Gibbons 144 posts 61 karma points
    Feb 25, 2010 @ 17:54
    Neil Gibbons
    0

    My usercontrol does this:

    HttpContext.Current.Items.Add("formstep", "1");

    My XSLT does this in an msxml script:

       public string GetFormConversion()
        {
            string s = "";
            foreach(object k in HttpContext.Current.Items.Keys)
            {
                s += (string)k + ", ";       
            }
            return s;
        }
    And this is displayed, but no "formstep"
    AspSessionIDManagerInitializeRequestCalled, UmbPage, umbOriginalUrl, VirtualUrl, pageID, UmbracoContext, AspSession, macrosAdded, UmbracoXmlContextContent, LiveEditingContext, pageElements, LiveEditing_LastItemId
    Neil

  • Alex Norcliffe 222 posts 287 karma points
    Feb 25, 2010 @ 21:58
    Alex Norcliffe
    0

    Hi Niel, have you tried just using a reference to the currently executing page?

    The ASP.NET (WebForms) page lifecycle is based primarily round events, not necessarily the order of their inclusion in the Control tree. So if you're setting a value in HttpContext.Items on Load in one control, by referencing it in another control you must be sure that is going to happen after the Load event has fired on the other one. Try setting the value in an Init event if possible.

    In any event, you should just be able to access the executing Handler which is likely to be a containing Page. You could try something like the following: 

    Page page = HttpContext.Current.Handler as Page;

    if (page != null)
    {
         
    // Use page instance, e.g. page.IsPostBack
    }

  • Alex Norcliffe 222 posts 287 karma points
    Feb 25, 2010 @ 22:02
    Alex Norcliffe
    1

    Oops just noticed you have that casting in your original post, but you're checking for IsValid rather than IsPostBack - confusing! What's the status of your issue atm?

  • Neil Gibbons 144 posts 61 karma points
    Feb 26, 2010 @ 08:50
    Neil Gibbons
    0

    Looking at re-writing the XSLT into .NET user control - or changing the form so it writes somethng to the querystring for my macro to pick up

    As you say Alex, I was able to get to the page via the handler, but the IsPostback was always False - even after I'd posted the form.

     

    Neil

  • Neil Gibbons 144 posts 61 karma points
    Feb 26, 2010 @ 09:00
    Neil Gibbons
    1

    Ah ha!

    Well, a minor step forward and future note to self - don't use Google Chrome to view source!
    It appears that in my build of Google Chrome, (5.0.355.0 dev)  - not sure if this affects the release version - when you view source the source view is actually a brand new request!

    • I load my page, check for HTML comment that displays postback = "False".
    • I submit my form - see confirmation screen, view source, comment still says "False", and the mark-up of the page is of the original form, not my response screen!
    Switching to IE and glueing back in what little hair I had that I'd been pulling out!

  • Chris Gaskell 59 posts 142 karma points
    Feb 26, 2010 @ 09:48
    Chris Gaskell
    0

    That's what you get for being an alternative developer Gibbons!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 26, 2010 @ 11:08
    Lee Kelleher
    0

    Luckily I noticed that Chrome "feature" quite early on. (So far it applies to all versions).  If you right-click and "Inspect Element" it will show you the current DOM (including any dynamic elements) - which is super useful for debugging HTML generated by various jQuery plug-ins!

  • Neil Gibbons 144 posts 61 karma points
    Feb 26, 2010 @ 14:17
    Neil Gibbons
    1

    One more top-tip everyone probably knows: Dont edit your XML files in Wordpad!

  • tarekahf 215 posts 153 karma points
    May 03, 2010 @ 09:04
    tarekahf
    0

    Hi Neil,

    I am facing same problem.

    The property IsPostBack is alwasy false when User Control Macro us run from inside Umbraco.

    Were you abel to solve this problem ?

    Tarek.

Please Sign in or register to post replies

Write your reply to:

Draft