Copied to clipboard

Flag this post as spam?

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


  • fed 199 posts 70 karma points
    Nov 19, 2009 @ 10:37
    fed
    1

    Macro parameters from codebehind

    Basically I have an ordinary .NET usercontrol (not a macro). In this usercontrol I have this:

    <umbraco:Macro id="ObjectList" Alias="ObjectList" runat="server" />

    and in codebehind of my usercontrol on PageLoad I try setting this macros parameters with this code:

    ObjectList.MacroAttributes.Add("Test", "macroparametertest");

    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.

    Macro mcr = new Macro();
    mcr.Alias = "ObjectList";
    mcr.MacroAttributes.Add("test", "macroparametertest");
    this.Controls.Add(mcr);

    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.

     

     

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Nov 19, 2009 @ 13:58
    Morten Christensen
    2

    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

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Nov 19, 2009 @ 14:00
    Morten Christensen
    1

    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.

  • Murray Roke 503 posts 966 karma points c-trib
    Nov 23, 2009 @ 21:15
    Murray Roke
    0

    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:

        <umbraco:Macro Alias="img" src='<%$ Code: "ProcessImage.aspx?src=" + Node.GetCurrent().GetProperty("UserImageFile").Value) %>' title="[#UserImageTitle]" alt="[#UserImageTitle]" width="600" height="310" runat="server"></umbraco:Macro>

    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.

  • fed 199 posts 70 karma points
    Nov 25, 2009 @ 15:07
    fed
    0

    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.

  • Nalle Jacobsson 32 posts 55 karma points
    Mar 24, 2010 @ 00:09
    Nalle Jacobsson
    0

    Would be nice if this could be fixed, it would add flexibility. Has it been added to Codeplex?

  • Nicolas 26 posts 46 karma points
    Sep 30, 2011 @ 17:37
    Nicolas
    0

    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?

  • David Conlisk 432 posts 1008 karma points
    Apr 17, 2013 @ 13:08
    David Conlisk
    1

    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:

    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)!

    Thanks,

    David

Please Sign in or register to post replies

Write your reply to:

Draft