这似乎工作正常:
模板:
<umbraco:Macro Alias="Sandbox" runat="server"></umbraco:Macro>
<asp:Label runat="server" ID="Label1" Text="Yo"></asp:Label>
宏脚本:
@using System.Web.UI.WebControls
@using umbraco.MacroEngines
@inherits DynamicNodeContext
@{
Page page = HttpContext.Current.Handler as Page;
page.Load += new EventHandler(page_Load);
}
@functions
{
static void page_Load(object sender, EventArgs e)
{
Page page = sender as Page;
Label label = FindControlRecursive(page, "Label1") as Label;
label.Text = "Hello";
}
private static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
}
注意:FindControlRecursive()
方法来自这个论坛帖子。