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?
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?
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)
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?
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.HandlerasPage;
if(page !=null) { // Use page instance, e.g. page.IsPostBack }
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?
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!
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!
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:
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
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?
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.
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
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.
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
Neil,
What's happening with your mxml:script? Doesn't it return the expected value?
Chris.
My usercontrol does this:
My XSLT does this in an msxml script:
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:
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?
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
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!
That's what you get for being an alternative developer Gibbons!
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!
One more top-tip everyone probably knows: Dont edit your XML files in Wordpad!
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.
is working on a reply...