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.
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.
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).
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.
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?
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
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
You might wrap it in a usercontrol in order to be able to call the Databind() method.
Have you tried using the <%= NumberToShow%> synntax?
Try this...works like a charm....
http://www.delphicsage.com/home/blog.aspx/d=854/title=Code_Expressions_to_Programmaticify_Your_Umbraco_Site
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.
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).
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.
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?
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
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
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
is working on a reply...