In the XSLT of my ObjectList-macro this should output: macroparametertest however, nothing seems to go through to the macro at all. My parameter is defined and I have tried setting it manually and it works.
It explains that the parameters should be specified in lowercase when you add them from codebehind, so I tried changing it to "test" instead, but nothing changed, I still don't get any output from my xslt-macro.
I then tried adding the control by creating a new macro and adding it to my usercontrol as munkimagik explains.
This however works great and thanks to munkimagik I have a working solution, but I am still curious as to why I can't set the property without adding the control dynamically.
Maybe I should add that I had to create the entire Macro in codebehind to get it working. I don't think having a part of the Macro in the .ascx and adding attributes from codebehind will work.
I believe the problem is that macros are evaluated very early in the page lifecycle.
I was never able to dynamically set properties in any of the normal ways.
However, I did discover the trick of using an "Expression Builder", this works because an expression builder is evaluated When the property is requested, as opposed to a specific point in the page lifecycle.
The solution I used is the CodeExpressionBuilder. which lets you insert dynamic properties like so:
Morten, as you see in my post that is what I ended up doing to get it working. I was only curious as to why my other way didn't work. But thanks for your input anyway..
Murray, thats what I figured. I am so used to be able to set stuff on servercontrols in codebehind, that however would be the best way of doing it.
Thanks for your tip about the expressionbuilder, will look into it.
This is still an issue in v4.11.6. Using Morten's tactic works though! Thanks Morten. It works in Page_Load too. Here's my working code:
var quote = new Macro { Alias = "Quote" }; quote.MacroAttributes.Add("quoteid", id); pnlWidgets.Controls.Add(quote);
Where pnlWidgets is a Panel in my user control and this code is running in Page_Load.
Note that when adding the MacroAttribute it must be lowercase, regardless of the settings for the Umbraco Macro (as Fed mentioned in the original post)!
Macro parameters from codebehind
Basically I have an ordinary .NET usercontrol (not a macro). In this usercontrol I have this:
and in codebehind of my usercontrol on PageLoad I try setting this macros parameters with this code:
In the XSLT of my ObjectList-macro this should output: macroparametertest however, nothing seems to go through to the macro at all. My parameter is defined and I have tried setting it manually and it works.
I then found this blogpost: http://munkimagik.wordpress.com/2009/04/08/adding-umbraco-macro-dynamically-to-user-control/
It explains that the parameters should be specified in lowercase when you add them from codebehind, so I tried changing it to "test" instead, but nothing changed, I still don't get any output from my xslt-macro.
I then tried adding the control by creating a new macro and adding it to my usercontrol as munkimagik explains.
This however works great and thanks to munkimagik I have a working solution, but I am still curious as to why I can't set the property without adding the control dynamically.
Hi,
Think I recently had the same problem as you, and I solved it by by creating a placeholder on the .ascx file:
<asp:PlaceHolder ID="macroContainer" runat="server"/>
In my codebehind I use OnPreRender (can't remember if there were any difference in putting it in PageLoad, but I got this working):
protected override void OnPreRender(EventArgs e)
{
employee = Member.GetMemberFromEmail(Request.QueryString["email"]);
if (employee == null) return;
mcr.Alias = "BreadcrumbNav";
mcr.MacroAttributes.Add("isemployeepage", "true");
mcr.MacroAttributes.Add("memberid", employee.Id);
this.macroContainer.Controls.Add(mcr);
}
Hope it helps or in some way provide inspiration for solving your problem.
- Morten
Maybe I should add that I had to create the entire Macro in codebehind to get it working. I don't think having a part of the Macro in the .ascx and adding attributes from codebehind will work.
So try moving the entire Macro to codebehind.
Hi Fed,
I believe the problem is that macros are evaluated very early in the page lifecycle.
I was never able to dynamically set properties in any of the normal ways.
However, I did discover the trick of using an "Expression Builder", this works because an expression builder is evaluated When the property is requested, as opposed to a specific point in the page lifecycle.
The solution I used is the CodeExpressionBuilder. which lets you insert dynamic properties like so:
Something else, that I didn't use but you may wish to investigate is here: http://www.imaginaryrealities.com/post/2009/01/15/Item-ExpressionBuilder-extension-for-Umbraco.aspx
Cheers.
Murray.
Morten, as you see in my post that is what I ended up doing to get it working. I was only curious as to why my other way didn't work. But thanks for your input anyway..
Murray, thats what I figured. I am so used to be able to set stuff on servercontrols in codebehind, that however would be the best way of doing it.
Thanks for your tip about the expressionbuilder, will look into it.
Would be nice if this could be fixed, it would add flexibility. Has it been added to Codeplex?
Hi,
I have the same problem. I'll try the CodeExpressionBuilder solution, but I find it terribly ugly! Since last year, is the a new solution?
Hi all,
This is still an issue in v4.11.6. Using Morten's tactic works though! Thanks Morten. It works in Page_Load too. Here's my working code:
Where pnlWidgets is a Panel in my user control and this code is running in Page_Load.
Note that when adding the MacroAttribute it must be lowercase, regardless of the settings for the Umbraco Macro (as Fed mentioned in the original post)!
Thanks,
David
is working on a reply...