Copied to clipboard

Flag this post as spam?

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


  • Mark Olbert 87 posts 117 karma points
    Feb 14, 2010 @ 03:11
    Mark Olbert
    0

    Pass Dynamic Value to Macro

    I want to do something conceptually like this:

        <umbraco:Macro numToShow="<%# NumberToShow %>" Alias="MostRecentNews" runat="server"></umbraco:Macro>

    where the value of the macro parameter numToShow is pulled from the code-behind for the template in which the macro resides.

    Now, the code above won't work, because <%#...%> is a data-binding expression, and macros don't databind. At least, I don't think they do.

    Is there a way to do what I'm trying to achieve here? I looked into code expressions, but they aren't "dynamic": if you change the value of NumberToShow (say, from clicking a link on the page) it's not reflected on postback. Apparently the code expression only gets parsed once, when the page is initially compiled. Which won't do what I need.

    - Mark

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 14, 2010 @ 04:52
    Morten Bock
    1

    You might wrap it in a usercontrol in order to be able to call the Databind() method. 

    Have you tried using the <%= NumberToShow%> synntax?

  • smercer 39 posts 67 karma points
    Feb 14, 2010 @ 04:53
  • smercer 39 posts 67 karma points
    Feb 14, 2010 @ 04:55
    smercer
    0

    P.S. You example is a bind time directive, which is not appropriate. Also, what morten recommends is too late in the page lifecycle, macro params need to be set pre-init. Code expressions are the best way to get around this.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Feb 14, 2010 @ 07:47
    Aaron Powell
    0

    DataBind isn't called by default on a control, you need to explicitly invoke it yourself. This is why you have to explicitly invoke DataBind of any .NET control after setting a DataSource (unless you're specifying the ID of a data source control).

  • Mark Olbert 87 posts 117 karma points
    Feb 16, 2010 @ 19:04
    Mark Olbert
    0

    I'm aware of the need to call DataBind() manually. But I didn't see how to do that against an umbraco macro. How do I reference/find an Umbraco macro in the code-behind?

    As regads code expressions, as I mentioned I tried them, but they appear to only get parsed the first time the page is created. In other words, they pull whatever value is in effect at the time they're parsed, but if you change that value on postback the new value is not reflected.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 16, 2010 @ 23:52
    Morten Bock
    0

    If you just call Databind() in the page_load, then it will databind all controls on the page, as far as I recall?

    I haven't tried it though, so I might be wrong. 

    But then again, if you are not using any other parameters, and you don't use the caching features, the you might just insert the usercontrol directly without the macro wrapping?

  • Fredrik Sewén 39 posts 106 karma points
    Mar 25, 2010 @ 12:02
    Fredrik Sewén
    0

    I've tried all kinds of databind expressions. In the end I always go back to using this server-side approach:

    http://munkimagik.wordpress.com/2009/04/08/adding-umbraco-macro-dynamically-to-user-control/

    /Fredrik

  • Brian Holmgård Kristensen 1 post 21 karma points
    Nov 17, 2011 @ 08:34
    Brian Holmgård Kristensen
    0

    Please see below for a solution on the databinding issue - possibly not supporting all scenarios, but at least it supported mine :)

     

    public class DataBindingSupportingMacro : UserControl

    {

    public string Alias { get; set; }

    protected override void DataBindChildren()

    {

    base.DataBindChildren();

    var macro = new Macro

    {

       Alias = Alias

    };

    foreach (string attributeKey in Attributes.Keys)

    macro.Attributes.Add(attributeKey, Attributes[attributeKey]);

    Controls.Add(macro);

    }

    }

     

    Best Regards,
    Brian Holmgård Kristensen 

  • Adam Jenkin 71 posts 226 karma points
    Feb 03, 2012 @ 12:21
    Adam Jenkin
    0

    Anybody been able to have much more luck with this - I get there are some work arounds but I would just like to use a traditional databind expression or begrudginly I dont mind setting attribute from code behind and databinding. Im on 4.7.1 and can currently get neither to work

Please Sign in or register to post replies

Write your reply to:

Draft